Home > Back-end >  problems with POST request google apps script
problems with POST request google apps script

Time:07-05

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.

CodePudding user response:

try

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

CodePudding user response:

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);
  });

CodePudding user response:

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:

  • Related