I’ve been trying to modify an item related value using a store’s API and although it returns success, the raw object is not bring the values correctly and I can’t find out why.
The Script Editor logs show:
Raw data: {"tipo":3,"quantidade":10} …which is correct.
The response I get is: "code":"010","message":"success","responseCode":200,
The destination log shows:
{"erp_id":"","peso":"","gtin":"","ativo":"","tipo":"","quantidade":"", …where tipo and quantidade are empty – thus, incorrect.
Here’s the piece of code I’m working on:
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)
}
}
Any idea as to why raw data is not going through all the way?
Thank you!
>Solution :
In the UrlFetchApp options, the request body is indicated by payload rather than body:
const requestOptions = {
"method": 'PUT',
"headers": headers,
"payload": raw, // change this line
'followRedirects': true // and this one
};
Reference: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)