向Web应用程序发出POST请求会运行doPost,但收到400错误响应。

huangapple go评论90阅读模式
英文:

Making a POST request to Web App runs doPost, but getting 400 error in response

问题

在我的Google云平台(Cloud Logs)日志中,我可以看到请求已经被接收并且doPost函数已经运行,根据函数的时间戳和日志记录:

function doPost(e) {

  let request = JSON.parse(e.postData.contents)

  request = request.replace(/-/g, "+").replace(/_/g, "/").replace(/%3D/g, "=")
  request = forge.util.decode64(request) 

  console.log("request")
  console.log(request)

  const props = PropertiesService.getScriptProperties()
  props.setProperty(request.clientId, request.email)

  return ContentService.createTextOutput(`{
      "data": "success", 
      "code": 200
    }`).setMimeType(ContentService.MimeType.JSON)
  
}

但是我从Apps Script得到的响应似乎是因为我最初发出的请求无效而导致的:

{
   "headers":{
      
   },
   "body":"<!DOCTYPE html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n  <title>Error 400 (Bad Request)!!1<\/title>\n  <style>\n    {margin:0;padding:0}html,code{font:15px\/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px} > body{background:url(\/\/www.google.com\/images\/errors\/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/1x\/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) no-repeat 0% 0%\/100% 100%;-moz-border-image:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n  <\/style>\n  <a href=\/\/www.google.com\/><span id=logo aria-label=Google><\/span><\/a>\n  <p><b>400.<\/b> <ins>That\u2019s an error.<\/ins>\n  <p>Your client has issued a malformed or illegal request.  <ins>That\u2019s all we know.<\/ins>\n",
   "response":{
      "code":400,
      "message":"Bad Request"
   },
   "cookies":[
      
   ],
   "filename":null,
   "http_response":{
      "data":null,
      "headers":null,
      "status":null
   }
}

尽管我可以在GCP中看到doPost的日志:

向Web应用程序发出POST请求会运行doPost,但收到400错误响应。

我在WordPress站点中还有另一个POST请求,请求的详细信息除了请求对象之外都相同。正常工作的有效载荷如下:

$request_obj = array(
  'data' => array(
    'nonce' => $nonce,
    'client_name' => $company_name,
    'product' => $payment_data->product_name,
    'license_information' => array(
      'subscription_id' => $license_data->id,
      'end_of_billing_cycle' => $payment_data->current_period_end,
      'last_payment' => $payment_data->current_period_start,
      'product' => $payment_data->product
    )
  )
);

在什么情况下,doPost仍然运行,但会抛出400错误?我感到有些困惑,因为通常如果HTTP请求无效,Apps Script代码就不会运行。

英文:

Scenario:

I have a WordPress website set up on an AWS server. I'm making a POST request to a Google Apps Script Web App and attempting to log the response. My PHP code is as follows:

$request_obj = array(
	'data' => array(
		'client_id' => $client_id,
        'email' => $email
	)
);

$request_data = json_encode($request_obj);

$url = WEB_APP_URL;

$webSafe = str_replace(['+', '/'], ['-', '_'], base64_encode($request_data));

$args = array(
	'method' => 'POST',
	'headers' => array(
		'Content-Type' => 'application/json'
	),
	'body' => array(
		'data' => $webSafe
	)
);

$response = wp_remote_post($url, $args);	

In my Cloud Logs in GCP, I can see that the request is being received and the doPost function is being run, as per the timestamps and logs of the function

function doPost(e) {

  let request = JSON.parse(e.postData.contents)

  request = request.replace(/-/g, "+").replace(/_/g, "/").replace(/%3D/g, "=")
  request = forge.util.decode64(request) 

  console.log("request")
  console.log(request)

  const props = PropertiesService.getScriptProperties()
  props.setProperty(request.clientId, request.email)

  return ContentService.createTextOutput(`{
      "data": "success", 
      "code": 200
    }`).setMimeType(ContentService.MimeType.JSON)
  
}

But the response I'm getting from Apps Script is as if the request I've made in the first place is invalid:

{
   "headers":{
      
   },
   "body":"<!DOCTYPE html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n  <title>Error 400 (Bad Request)!!1<\/title>\n  <style>\n    {margin:0;padding:0}html,code{font:15px\/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px} > body{background:url(\/\/www.google.com\/images\/errors\/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/1x\/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) no-repeat 0% 0%\/100% 100%;-moz-border-image:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n  <\/style>\n  <a href=\/\/www.google.com\/><span id=logo aria-label=Google><\/span><\/a>\n  <p><b>400.<\/b> <ins>That\u2019s an error.<\/ins>\n  <p>Your client has issued a malformed or illegal request.  <ins>That\u2019s all we know.<\/ins>\n",
   "response":{
      "code":400,
      "message":"Bad Request"
   },
   "cookies":[
      
   ],
   "filename":null,
   "http_response":{
      "data":null,
      "headers":null,
      "status":null
   }
}

Even though I can see the doPost logs in GCP:

向Web应用程序发出POST请求会运行doPost,但收到400错误响应。

I have another POST request being made from the WordPress site, the details of the request are the same except for the request object. The working payload is as such:

$request_obj = array(
'data' => array(
'nonce' => $nonce,
'client_name' => $company_name,
'product' => $payment_data->product_name,
'license_information' => array(
'subscription_id' => $license_data->id,
'end_of_billing_cycle' => $payment_data->current_period_end,
'last_payment' => $payment_data->current_period_start,
'product' => $payment_data->product
)
)
);

Question:

Under what circumstances would 400 error be thrown while the doPost still runs? I'm at a bit of a loss because normally the Apps Script code wouldn't run if the HTTP request was invalid.

答案1

得分: 0

我通过切换到curl并添加一个接受标头来使其工作:

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "accept: application/json"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

if (curl_errno($ch)) {
  file_put_contents($log_file, '发生错误: ' . curl_error($ch), FILE_APPEND);
}

$response = curl_exec($ch);
英文:

I got this working by switching to curl and adding an accept header:

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "accept: application/json"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

if (curl_errno($ch)) {
  file_put_contents($log_file, 'Error occurred: ' . curl_error($ch), FILE_APPEND);
}

$response = curl_exec($ch);

huangapple
  • 本文由 发表于 2023年7月24日 16:14:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定