Search code examples
xmlhttprequestautodesk-viewerautodesk-forgeautodesk-model-derivative

Http Status 415 even when providing correct ContentType


I'm attempting to set up a quick POC of an Autodesk Viewing App but I'm running into issues with authentication. I've already checked the following questions for general help but they're either covering extremely specific issues like HashTableMapping or haven't even been answered:

HTTP Status 415 - Unsupported Media Type when doing POST

HTTP status 415 when using Alamofire multipart upload

HTTP Error 500 when attempting to access localhost

The request failed with HTTP status 415

According to Autodesk's documentation my request structure is totally fine but seems to be throwing a 415 error. Anyone have any ideas or see something I'm not seeing? Here's my code below:

var XmlHttpRequest = require("xmlhttprequest").XMLHttpRequest;
console.log("references loaded");

var xhr = new XmlHttpRequest();
xhr.open("POST", "https://developer.api.autodesk.com/authentication/v1/authenticate", false);

var headerRequest = "Content-Type: application/x-www-form-urlencoded";
var headerValue = 
    "client_id=_someSecretValue_" + "&" +
    "client_secret=_someOtherSecretValue_" + "&" +
    "grant_type=client_credentials" + "&" +
    "scope=data:read";

xhr.setRequestHeader(headerRequest, headerValue);
xhr.send();

console.log(xhr.status); //getting a 415 here
console.log(xhr.statusText);

Solution

  • Got it - was my own faulty logic that screwed me up. The first request header wasn't formatted properly and I needed to send my information as a value:

    var XmlHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    console.log("references loaded");
    
    var xhr = new XmlHttpRequest();
    xhr.open("POST", "https://developer.api.autodesk.com/authentication/v1/authenticate", false);
    
    //var headerRequest = "Content-Type:application/x-www-form-urlencoded";
    var headerValue = 
        "client_id=_someSecretValue_" + "&" +
        "client_secret=_otherSecretValue_" + "&" +
        "grant_type=client_credentials" + "&" +
        "scope=data:read";
    
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(headerValue);
    
    console.log(xhr.status);
    console.log(xhr.statusText);