He estado tratando de modificar un valor relacionado con un artículo utilizando la API de una tienda y, aunque devuelve el éxito, el objeto sin raw no trae los valores correctamente y no puedo averiguar por qué.
Los registros del Editor de secuencias de comandos muestran: Raw data: {"tipo":3,"quantidade":10} ...que es correcto.
La respuesta que obtengo es: "code":"010","message":"success","responseCode":200,
El registro de destino muestra: {"erp_id":"","peso":"","gtin":"","ativo":"","tipo":"","quantidade":"", .. .donde tipo y quantidade están vacíos, por lo tanto, incorrectos.
Aquí está la pieza de código en la que estoy trabajando:
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) } }¿Alguna idea de por qué los datos sin procesar no se procesan por completo?
¡Gracias!
En las opciones de UrlFetchApp , el cuerpo de la solicitud se indica mediante la payload en lugar del body :
const requestOptions = { "method": 'PUT', "headers": headers, "payload": raw, // change this line 'followRedirects': true // and this one };Referencia: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(Cadena,Objeto)