Forum Discussion
POST request to Power Query
- 4 years ago
Hi All,
Finally managed to crack it - here is the request that I created which returns the API token:
Hi alexmoller
Try this code which I use to make POST request to other API's.
let
// Get the API Token
api_url = "https://sandbox.spearline.com/",
token_path = "admin/api/user/token",
Username = "xxxxxxxx",
Password = "xxxxxxxx",
EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(Username & ":" & Password), BinaryEncoding.Base64),
Token_Response = Json.Document(Web.Contents(api_url,
[
RelativePath = token_path,
Headers = [#"Content-Type"="application/json",#"Authorization"=EncodedCredentials],
Content=Text.ToBinary("grant_type=client_credentials")
]
)
),
// Get the token from the API response
token = Token_Response[token]
in
token
Obviously it will need some tweaking to work for you.
To make a POST you typically need to supply some Credentials in the request and this is done by the EncodedCredentials. However as I don't have any documentation for your API I don't know exactly how Spearline expects credentials to be supplied.
Based on the info you have supplied, in the Token_Response it looks like the token itself will be in a field called [token]. If it's something different then you just need to change that field name.
I've also set the Content-Type to application/json based on what you have said the API expects.
Give it a try and let me know how you go.
If you have documentation on the API please share as I can then check the exact settings required in the POST request.
regards
Phil
Thanks PhilipTreacy - much appreciated! I did try to adjust the code you gave me but I get a "We could not authenticate with the credentials you provided" error - I will do some digging and see if I can work out whats going on.
Unfortunately their documentation requires you to sign in 😞 - here is a couple of screenshots from the authentication process - hopefully that will give you a bit more of an idea
Let me know if you need any more info
- alexmoller4 years agoFrequent Visitor
Hi All,
Finally managed to crack it - here is the request that I created which returns the API token: