How do I use HTTP Basic Auth with http-client?

195 views Asked by At

I want to use http-client to make an HTTP request equivalent to:

curl -u 'user:pass' 'https://api.example.net/a/b/c'

I have read the docs for http-client, as well as for intarweb and uri-common, but I am still unsure of how to achieve this.

I can see that it's possible to set a username and password with make-uri, but I'm not sure if that's the best option.

I'm open to multiple solutions if there is more than one way to do it, but an idiomatic answer is preferable.

2

There are 2 answers

3
wasamasa On BEST ANSWER
(define user "user")
(define pass "pass")

(determine-username/password
 (lambda (uri realm)
   (values user pass)))

See https://wiki.call-cc.org/eggref/5/http-client#authentication-support for the technical details.

0
sjamaan On

As the docs say,

the default value is a procedure which extracts the username and password components from the URI"

So, setting the password with make-uri is definitely supported and probably the simplest way in general. This is why with-input-from-request and friends accept either a string, an URI object or a request - you pass in the simplest thing that can work.