shadowtalker
shadowtalker

Reputation: 13853

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

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.

Upvotes: 1

Views: 259

Answers (2)

sjamaan
sjamaan

Reputation: 2292

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.

Upvotes: 2

wasamasa
wasamasa

Reputation: 320

(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.

Upvotes: 2

Related Questions