简体   繁体   English

POST请求谷歌应用脚​​本的问题

[英]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.我尝试通过 POST 请求从 API 接收一些数据,并通过 google 应用程序脚本将其放在 Google Sheet 中。

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.我收到状态 200 OK 和标题。 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.但是当我尝试使用 response.getContent() 方法时,我收到了一堆数字,根据文档,这绝对不是我应该收到的。 I tried to send a request from Postman using URL from docs and got a proper response.我尝试使用文档中的 URL 从 Postman 发送请求并得到正确的响应。 Same with Curl request.与卷曲请求相同。 I'm a beginner and would appreciate help on how I can get the correct response data in this case.我是一个初学者,在这种情况下如何获得正确的响应数据,我将不胜感激。

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

try尝试

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

I believe your goal is as follows.我相信你的目标如下。

  • You want to convert the following curl command to Google Apps Script.您想将以下 curl 命令转换为 Google Apps 脚本。

     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' .在您的 curl 命令中,请求标头只有'accept: application/json' In this case, I thought that the content type might be application/x-www-form-urlencoded .在这种情况下,我认为内容类型可能是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.当这些点反映到 Google Apps 脚本时,它变成如下。

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.这个修改后的脚本假设您的 curl 命令可以正常工作。 Please be careful about this.请注意这一点。
  • The request of this modified script is the same with that of your curl command.这个修改过的脚本的请求和你的 curl 命令的请求是一样的。 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.并且,请再次确认您的api_key=My_Token&format=json&page=1&cdrs_per_page=25&sort_by=call_start&sort_direction=asc的值。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM