How can I send post request of Json data from javascript on Autodesk Tandem?

124 Views Asked by At

Here is the stream url : https://:[email protected]/api/v1/timeseries/models/urn:adsk.dtm:LudKiyWAQcCPqK2gQWxNfw/streams/AQAAAMTWX-rTaksXg95DuoCDE6UAAAAA

I've managed to send json data from Postman to the above URL without any authentication. I've try to send the same json data via Javascript. The error in the console is " Forbidden". I'm not sure if I need to provide any more credential when using the Javascript method. Below is my code :

  "https://tandem.autodesk.com/api/v1/timeseries/models/urn:adsk.dtm:LudKiyWAQcCPqK2gQWxNfw/streams/AQAAAMTWX-rTaksXg95DuoCDE6UAAAAA";
const data = [
  {
    Temperature: 25, // Replace with your temperature value
    // Replace with your value
  },
];

// Send the POST request
fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer :nYjvNhH3TKODSJuWL-0MTQ", // Your Bearer token
  },
  body: JSON.stringify(data),
})
  .then((response) => {
    // Check if the response is JSON
    const contentType = response.headers.get("content-type");
    if (contentType && contentType.indexOf("application/json") !== -1) {
      return response.json();
    } else {
      return response.text();
    }
  })
  .then((data) => {
    console.log("Success:", data);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
2

There are 2 best solutions below

0
Jan Liska On

the ingestion URL uses basic authentication. Instead of using Bearer token simply use basic authentication, i.e.:

fetch(url, {
    method: 'POST',
    body: JSON.stringify(payload),
    headers: {
        authorization: `Basic ${authHeaderValue}`,
    },
});

You can find complete example here.

0
gio91vy On

Tandem generates a API URL with inside username generic so ":", you can use whatever you like and the password in your case is "nYjvNhH3TKODSJuWL-0MTQ" and the actual api link is just:

https://tandem.autodesk.com/api/v1/timeseries/models/urn:adsk.dtm:LudKiyWAQcCPqK2gQWxNfw/streams/AQAAAMTWX-rTaksXg95DuoCDE6UAAAAA

hope it can help you