Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
Why is fetch not working propely using Google Apps Script?

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!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda