Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!