postman 如何在Autodesk Tandem上从JavaScript发送JSON数据的post请求?

jxct1oxe  于 5个月前  发布在  Postman
关注(0)|答案(1)|浏览(59)

以下是流URL:https://:email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)/API/v1/timeseries/models/urn:adsk.dtm:LudKiyWAQcCPqK 2gQWxNfw/streams/AQAAAMTWX-rTaksXg 95 DuoCDE 6 UAAAAA
我已经成功地从Postman发送json数据到上面的URL,而没有任何身份验证。我尝试通过JavaScript发送相同的json数据。控制台中的错误是“禁止”。我不确定使用Javascript方法时是否需要提供更多的凭据。下面是我的代码:

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

字符串

kadbb459

kadbb4591#

摄取URL使用基本身份验证。而不是使用承载令牌简单地使用基本身份验证,即:

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

字符串
你可以找到完整的例子here

相关问题