Forum Discussion

th3h0bb5's avatar
th3h0bb5
Icon for Resolver II rankResolver II
7 years ago

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

  • Stachu's avatar
    Stachu
    Icon for Community Champion rankCommunity 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

    • th3h0bb5's avatar
      th3h0bb5
      Icon for Resolver II rankResolver II

      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" }

       

       

      • Stachu's avatar
        Stachu
        Icon for Community Champion rankCommunity 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? 

  • 5AMsan's avatar
    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.