3

I'm a beginner and I want to write a Nim-application that processes some data from an internal website. Basic authentication (username, password) is required to access this site.

A working Python solution is:

response = requests.get('https://internal:PORT/page',
                        auth=('user', 'passwd'),
                        verify=False) # this is vital

Based on the nim doc regarding httpclient and the modules source code, where it is stated that one could use a proxy as an argument for any of the functions, I've been trying something along these lines:

var 
  client = newHttpClient()
  prox = newProxy("https://internal:PORT/page", "user:passwd")

let response = client.getContent(prox) # Error: type mismatch

The solution is probably very obvious but I'm out of ideas on how to authenticate.

If anybody could help, that'd be highly appreciated!

Daniel D.
  • 33
  • 4

1 Answers1

4

Basic auth is just an "Authorization" header with value "Basic " + base64(username + ":" + password). Equivalent in nim:

import httpclient, base64
var
    client = newHttpClient()
var username = ...
var password = ...
client.headers["Authorization"] = "Basic " & base64.encode(username & ":" & password)
# ... send request with the client
uran
  • 1,346
  • 10
  • 14
  • 1
    Worked like a charme. I'm going to do some more research on headers and the like to improve as a developer. Thanks a lot! – Daniel D. Dec 14 '17 at 12:24