Search code examples
uriwebclientfantom

Fantom loging to a remote web in code


Hi I'm really new to Fantom and I'm trying to do a few of things. I desire to access a data response in a server with a uri.

Void gets(){

str := WebClient(webpage).getStr
}

But when I try to connect I revie a 401 error. What's the best way for to send the loggin and password to the server?

Thanks for read


Solution

  • It all depends on what authentication is used by the server.

    Example, if it the server uses Basic HTTP Authentication then you need to set the Authorization HTTP header:

    using web
    
    class Example {
      Void main() {
        client := WebClient(`http://www.example.com/`)
        client.reqHeaders["Authorization"] = "Basic " + "username:password".toBuf.toBase64
        result := client.getStr
      }
    }
    

    If it uses a different authentication method, then you'll need to read up and implement that.

    Note that you need to create a new WebClient instance for each HTTP request, else you'll get strange errors!