Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
th3h0bb5
Resolver II
Resolver II

API error 415: Unsupported Media Type

I'm wanting to connect to an API that provides a temporary auth token but am getting a 415: Unsupported media type error when I try to retrieve the token. Here's my code:

 

let
    authURL = "https://api.com/auth,
    objectURL = "https://api.com/object",
    authJSON = Json.FromValue(
                [username="XXX", 
                password="YYY"]
                ),
    postAuth = Web.Contents(
                authURL, 
                [Content = authJSON]
                ),
    token = Json.Document(postAuth),
    Source = Web.Contents(
                objectURL,
                [Headers = [#"api_Key" = token]]
                )
in
    Source

I feel like the steps I'm going through are correct. I POST the JSON username/password, and it should return the token I need to pass when I do the rest of the API call. Anyone know what I'm doing wrong?

8 REPLIES 8
5AMsan
Frequent Visitor

For search engine results people who found this page, it's generally a matter of bad headers, you must declare sent data types ( ie. #"Content-type" = "Application/json" for JSON) in your header when using POST method.

 

Stachu
Community Champion
Community Champion

do you manage to connect normally with the default query? if yes, what's the syntax?

it seems to me that you're passing json as an text argument (e.g. with Content = authJSON), which might have caused the error



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi Stachu, I've been testing this connection with Postman (an API tester) and it works fine. I agree that it's something about the POST request that is causing my issue, but I'm not skilled enough in M to know why. I feel I am missing a conversion of some kind. Here are the steps I need to go through as I understand them:

  • Connect to an auth URL (http://url.api/auth)
  • POST a JSON script containing my username/password
    • { "username" : "user", "password" : "pass123" }
  • Retrieve and store the resulting token from the body of a JSON file:
    • { "token" : "abc123" }
  • Connect to a object URL (http://url.api/object)
  • GET the object by passing through the token in a JSON header:
    • { "api_key" : "abc123" }

 

 

example.png

Stachu
Community Champion
Community Champion

what's the format of the temporary authorization token?
if you go step by does step 

    postAuth = Web.Contents(
                authURL, 
                [Content = authJSON]
                ),

return a proper value, or does it return an error? if it works which step exactly is not working? 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

The postAuth step is the step that doesn't work, returning this error:
error.png

 

What it should be returning is a JSON token that looks like so:

{ "token" : "abc123" }

Stachu
Community Champion
Community Champion

the way M works you need to get address to the status where it could be opened in the browser with desired result
https://docs.microsoft.com/en-us/powerquery-m/web-contents

Web.Contents("www.microsoft.com") equals The binary contents from the URL www.microsoft.com when accessed via HTTP

I think in this case you could try with something like this:

    postAuth = Web.Contents(
                authURL, 
                [username="XXX", 
                password="YYY"]
                )

or 

    postAuth = Web.Contents(
                authURL & "username=XXX&password=YYY"
                )

if you can get it working in that format then taking the references from JSON is trivial
I'm afraid I cannot help more without any documentation on the API itself

 



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi Stachu,

 

First let me say thank you for taking the time to help me with this. I deeply appreciate it.

 

Web.Contents() by default is uses the GET method. However, my API requires that you POST some JSON containing your username/password. Using your first suggestion below results in an error because [username="xxx", password="yyy"] is not one of the arguments that Web.Contents() accepts. I am specifically using the Content=[] argument because that changes the Web.Contents method from GET to POST, which is what I need to do (see prior link for reference). The authentication JSON needs to be in the body, which is why I'm not using Header or paramaters.

 

Your suggestion did make me think that perhaps I need to nest Web.Contents: once to POST and then once to GET. I've been trying that, but no luck so far.

Stachu
Community Champion
Community Champion

Chris Webb did bunch of articles on the API handling in Power Query
https://blog.crossjoin.co.uk/2014/03/26/working-with-web-services-in-power-query/

https://social.msdn.microsoft.com/Forums/en-US/029fed1d-56fe-476a-9d7d-f367c5036b6e/posting-xml-to-a...

there are some examples there, so maybe it will help you get the proper syntax

 

there is also example with POST authentication here
https://chris.koester.io/index.php/2015/07/16/get-data-from-twitter-api-with-power-query/
that uses Text.ToBinary/Binary.ToText transformations - worth giving it a try 🙂



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.