Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

try

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda