#1723 WebClient Basic Authentication

volker Fri 9 Dec 2011

Hello,

I was playing around a bit with fantom.

Very simple example, using the WebClient to get some data from a rest API. But I was struggling with Basic Authentication.

Void sendStart()
{
  uri := Uri.fromStr("http://guest:guest@host:55672/api/vhosts")
  echo (uri.userInfo)
  wc := WebClient(uri)
  //wc.proxy = Uri.fromStr("http://webproxy:8000", true)
  status := wc.getStr
  echo (status)
}

I always get an 401.

I know there could be one million problems in my network/infrastructure. But when using curl or wget everything works fine. Has anybody really tested Basic Authentication with WebClient ?

Thanks

Volker

brian Fri 9 Dec 2011

@volker

Welcome to Fantom. If you want to use basic authentication with WebClient you can do it with a line like this:

// compute HTTP Basic auth string
wc := WebClient(uri)
wc.reqHeaders["Authorization"] = "Basic " + "$user:$pass".toBuf.toBase64

volker Fri 9 Dec 2011

Works fine, thanks a lot! The auth and userInfo methods of Uri confused me a bit, I thought their might be a convenient way for Basic Authentication ;)

I'm looking forward to learn a bit more Fantom, so be gentle, I'm trying not to ask too stupid questions.

Login or Signup to reply.