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

180
Views
problems with POST request google apps script

I try to receive some data from API with POST request and put it at Google Sheet via google apps script.

Here Curl example from docs:

    curl -X 'POST' \
  'https://example.com/api/cdrs?api_key=My_Token&format=json&page=1&cdrs_per_page=25&sort_by=call_start&sort_direction=asc' \
  -H 'accept: application/json' \
  -d ''

Here my code:

let options = {
    'method': 'POST',
    'contentType': 'application/json'
  }

  let response = UrlFetchApp.fetch(url, options)
  let status   = response.getResponseCode()
  let headers  = response.getHeaders()


  console.log(status,headers)
  console.log(response)
  console.log(response.getContent())

I receive status 200 OK and headers. Here headers that were returned:

Content-Security-Policy': 'frame-ancestors https://example.com',
  Connection: 'keep-alive',
  'alt-svc': 'h3=":443"; ma=86400, h3-29=":443"; ma=86400',
  'cf-ray': '725a8d94abb4825a-IAD',
  'Content-Encoding': 'gzip',
  'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  Server: 'cloudflare',
  'Content-Type': 'text/html; charset=UTF-8',
  Date: 'Mon, 04 Jul 2022 20:18:57 GMT',
  'Transfer-Encoding': 'chunked',
  'cf-cache-status': 'DYNAMIC'

When I try to console response I got this:

{ toString: [Function],
  getResponseCode: [Function],
  getContent: [Function],
  getHeaders: [Function],
  getContentText: [Function],
  getAllHeaders: [Function],
  getBlob: [Function],
  getAs: [Function] }

But when I tried to use response.getContent() method I received a bunch of numbers and this is defiantly not what I should receive according to the docs. I tried to send a request from Postman using URL from docs and got a proper response. Same with Curl request. I'm a beginner and would appreciate help on how I can get the correct response data in this case.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script.

      curl -X 'POST' \
      'https://example.com/api/cdrs?api_key=My_Token&format=json&page=1&cdrs_per_page=25&sort_by=call_start&sort_direction=asc' \
      -H 'accept: application/json' \
      -d ''
    

In your curl command, the request header has only 'accept: application/json'. In this case, I thought that the content type might be application/x-www-form-urlencoded. And, it seems that '' is required to be sent as the data. And, about other parameters, it seems that it is required to be sent as the query parameter.

When these points are reflected to a Google Apps Script, it becomes as follows.

Modified script:

function myFunction() {
  const url = "https://example.com/api/cdrs?api_key=My_Token&format=json&page=1&cdrs_per_page=25&sort_by=call_start&sort_direction=asc";
  const options = {
    method: "post",
    headers: { accept: "application/json" },
    payload: "",
  };
  const res = UrlFetchApp.fetch(url, options);
  console.log(res.getContentText())
}

Note:

  • This modified script supposes that your curl command works fine. Please be careful about this.
  • The request of this modified script is the same with that of your curl command. But, if an error occurs, please provide it. And, please confirm your values of api_key=My_Token&format=json&page=1&cdrs_per_page=25&sort_by=call_start&sort_direction=asc, again.

Reference:

  • fetch(url, params)
about 4 years ago · Juan Pablo Isaza Report

0

You need the complete the received data inform in the fetch the sequence of promises with:

let response = UrlFetchApp.fetch(url, {
  method: "POST",
  body: JSON.stringify(),
  headers: { "Content-Type": "application/json" },
})
  .then((response) => {
    var data = response.json();
    console.log(data);
  })
  .catch((err) => {
    console.log(err.message);
  });
about 4 years ago · Juan Pablo Isaza Report

0

try

let options = { 'method': 'POST', 'contentType': 'application/json' headers: { 'accept': 'application/json' }, payload: '' }

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!