Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Error with Web.Contents and POSTing to a URL that uses basic authentication

For each row in my data I need to do a Web API call to check on some house characteristics for the address in my table.  However, the WebAPI I am working with requires the address data to be POSTed a...
  • PhilipTreacy's avatar
    5 years ago

    Hi Anonymous 

    You can set the connection to Anonymous and then specify Basic auth in the Headers like so

     

     

    Headers = [ #"Authorization"=EncodedCredentials, #"Content-Type"="application/x-www-form-urlencoded" ], Content = Text.ToBinary(TheContent)    

     

     

    where the EncodedCredentials are 

     

     

    Credentials = "USERNAME:PASSWORD",
        EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(Credentials), BinaryEncoding.Base64),

     

     

     

    Here's the full snippet which includes your single line of code from your query above.  You will need to integrate this into your query at the appropriate place

     

     

    let
        URL = "https://mywebsite.com/XXXXXXXXXXXX/HTTPRequestListener",
        Credentials = "USERNAME:PASSWORD",
        EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(Credentials), BinaryEncoding.Base64),
    
        WebRequest = (TheContent) =>
    
        let
    
            Options = 
    
                [
                    Headers = [ #"Authorization"=EncodedCredentials, #"Content-Type"="application/x-www-form-urlencoded" ], Content = Text.ToBinary(TheContent)    
                ],  
    
            Result = Web.Contents(URL, Options)
    
        in
            Result,
    
    
    
    
        WebContents = Table.AddColumn(#"Create POST XML", "Roof Shape Response", each WebRequest([POST XML]))
    
    in WebContents

     

     

    Phil


    If I answered your question please mark my post as the solution.
    If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.