Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I authenticate, using Nim's httpclient module to retrieve HTML?

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!

like image 406
Daniel D. Avatar asked Apr 18 '26 19:04

Daniel D.


1 Answers

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
like image 98
uran Avatar answered Apr 21 '26 11:04

uran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!