Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

How to get results from this particular POST HTTP call in Google Script - works fine in python

I was given this simple script in python

import requests

val = "id"

data = {
  'val': val,
  'type': '1'
}

session = requests.session()

response = session.post('https://www.someweb.com/modules/output/ajax.check_results.php', data=data)

print(response.json())

For security reasons I am not allowed to reveal the correct url.

I do not know python (I can run above code on my computer and it works perfectly) so I wanted to port the code to Google Application Script. Currently it looks like that

function getResults() {
  
  var url = 'https://www.someweb.com/modules/output/ajax.check_results.php';

  var data = {
    'val': 'id',
    'type': '1'
  }
  var options = {
      "method": "post",
 //     "headers": headers,
      "data": data
  };
  var response = UrlFetchApp.fetch(url, options);

  Logger.log(response.getContent);
  Logger.log(response);
  Logger.log(response.getAllHeaders());
  Logger.log(response.getResponseCode());
  Logger.log(response.getContentText());
  //Logger.log(JSON.parse(response.getContent()));
  //Logger.log(JSON.parse(response.getContentText()));
  //Logger.log(JSON.parse(response));
} 

The output looks like that

2:47:22 PM Notice  Execution started
2:47:22 PM Info    function () { [native code] }
2:47:22 PM Info 
2:47:22 PM Info    {Content-Type=text/html; charset=UTF-8, Expires=Thu, 19 Nov 1981 08:52:00 GMT, Date=Sun, 24 Jan 2021 13:47:22 GMT, Set-Cookie=PHPSESSID=ibdotvlrrec1q6glnbe8i0iu23; path=/, Server=Apache/2.4.38 (Debian), Cache-Control=private, must-revalidate, Content-Length=0, Pragma=no-cache, keep-alive=timeout=5, max=100, Connection=Keep-Alive}
2:47:22 PM Info    200.0
2:47:22 PM Info
2:47:23 PM Notice  Execution completed

the commented Loggers give me that

3:14:03 PM Error SyntaxError: Unexpected end of JSON input  getResults @ 

I think that the issue is that Google Script takes a second to finish but python takes 14 seconds. It waits to get the results.

Someone could help how to make Google Script to get the results too?

question from:https://stackoverflow.com/questions/65871676/how-to-get-results-from-this-particular-post-http-call-in-google-script-works

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at the documentation for UrlFetchApp.fetch(). data isn't one of the available parameters. You should use payload instead and pass data as a string.

function getResults() {
  const url = 'https://www.someweb.com/modules/output/ajax.check_results.php';
  const data = {
    'val': 'id',
    'type': '1'
  };
  const options = {
    'method': 'post',
    'payload': JSON.stringify(data)
  };
  const response = UrlFetchApp.fetch(url, options);
  console.log(response.getContentText()); // string
  console.log(JSON.parse(response.getContentText())); // object, if it can be parsed
}

The error you received was from JSON.parse(response.getContent()), because JSON.parse() expects a string, but getContent() returns an array of raw binary data. As per my example above, you should use getContentText() instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...