我一直在嘗試使用商店的 API 修改與專案相關的值,雖然它回傳成功,但該raw物件沒有正確地帶來值,我不知道為什么。
腳本編輯器日志顯示:
Raw data: {"tipo":3,"quantidade":10}...這是正確的。
我得到的回應是: "code":"010","message":"success","responseCode":200,
目標日志顯示:
{"erp_id":"","peso":"","gtin":"","ativo":"","tipo":"","quantidade":"",...where tipoand quantidadeare empty - 因此,不正確。
這是我正在處理的一段代碼:
function addToStock() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Estoque PA');
const id = sheet.getRange(2, 3).getValue();
let quantidade = sheet.getRange(2, 5).getValue();
const variacao = sheet.getRange(2, 6).getValue();
const raw = JSON.stringify({
'tipo': 3,
'quantidade': quantidade
});
const headers = {
'Content-Type': 'application/json',
'Authorization': "Bearer " API_KEY
};
const requestOptions = {
"method": 'PUT',
"headers": headers,
"body": raw,
"redirect": 'follow'
};
Logger.log('Raw data: ' raw)
const url = `https://api.wwwww.com.br/v1/product/stock/${id}/${variacao}`;
Logger.log('URL: ' url)
const response = UrlFetchApp.fetch(url, requestOptions);
const getText = response.getContentText();
Logger.log('GetText: ' getText);
} catch (err) {
Logger.log('Error: ' err)
}
}
關于為什么原始資料沒有一路經過的任何想法?
謝謝!
uj5u.com熱心網友回復:
在UrlFetchApp選項中,請求正文由payload而不是body:
const requestOptions = {
"method": 'PUT',
"headers": headers,
"payload": raw, // change this line
'followRedirects': true // and this one
};
參考:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415110.html
標籤:
下一篇:一個url但對失敗的不同回應
