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

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

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

问题

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

  1. function doPost(e) {
  2. let request = JSON.parse(e.postData.contents)
  3. request = request.replace(/-/g, "+").replace(/_/g, "/").replace(/%3D/g, "=")
  4. request = forge.util.decode64(request)
  5. console.log("request")
  6. console.log(request)
  7. const props = PropertiesService.getScriptProperties()
  8. props.setProperty(request.clientId, request.email)
  9. return ContentService.createTextOutput(`{
  10. "data": "success",
  11. "code": 200
  12. }`).setMimeType(ContentService.MimeType.JSON)
  13. }

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

  1. {
  2. "headers":{
  3. },
  4. "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",
  5. "response":{
  6. "code":400,
  7. "message":"Bad Request"
  8. },
  9. "cookies":[
  10. ],
  11. "filename":null,
  12. "http_response":{
  13. "data":null,
  14. "headers":null,
  15. "status":null
  16. }
  17. }

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

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

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

  1. $request_obj = array(
  2. 'data' => array(
  3. 'nonce' => $nonce,
  4. 'client_name' => $company_name,
  5. 'product' => $payment_data->product_name,
  6. 'license_information' => array(
  7. 'subscription_id' => $license_data->id,
  8. 'end_of_billing_cycle' => $payment_data->current_period_end,
  9. 'last_payment' => $payment_data->current_period_start,
  10. 'product' => $payment_data->product
  11. )
  12. )
  13. );

在什么情况下,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:

  1. $request_obj = array(
  2. 'data' => array(
  3. 'client_id' => $client_id,
  4. 'email' => $email
  5. )
  6. );
  7. $request_data = json_encode($request_obj);
  8. $url = WEB_APP_URL;
  9. $webSafe = str_replace(['+', '/'], ['-', '_'], base64_encode($request_data));
  10. $args = array(
  11. 'method' => 'POST',
  12. 'headers' => array(
  13. 'Content-Type' => 'application/json'
  14. ),
  15. 'body' => array(
  16. 'data' => $webSafe
  17. )
  18. );
  19. $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

  1. function doPost(e) {
  2. let request = JSON.parse(e.postData.contents)
  3. request = request.replace(/-/g, "+").replace(/_/g, "/").replace(/%3D/g, "=")
  4. request = forge.util.decode64(request)
  5. console.log("request")
  6. console.log(request)
  7. const props = PropertiesService.getScriptProperties()
  8. props.setProperty(request.clientId, request.email)
  9. return ContentService.createTextOutput(`{
  10. "data": "success",
  11. "code": 200
  12. }`).setMimeType(ContentService.MimeType.JSON)
  13. }

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

  1. {
  2. "headers":{
  3. },
  4. "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",
  5. "response":{
  6. "code":400,
  7. "message":"Bad Request"
  8. },
  9. "cookies":[
  10. ],
  11. "filename":null,
  12. "http_response":{
  13. "data":null,
  14. "headers":null,
  15. "status":null
  16. }
  17. }

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:

  1. $request_obj = array(
  2. 'data' => array(
  3. 'nonce' => $nonce,
  4. 'client_name' => $company_name,
  5. 'product' => $payment_data->product_name,
  6. 'license_information' => array(
  7. 'subscription_id' => $license_data->id,
  8. 'end_of_billing_cycle' => $payment_data->current_period_end,
  9. 'last_payment' => $payment_data->current_period_start,
  10. 'product' => $payment_data->product
  11. )
  12. )
  13. );

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并添加一个接受标头来使其工作:

  1. $ch = curl_init();
  2. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "accept: application/json"));
  3. curl_setopt($ch, CURLOPT_URL, $url);
  4. curl_setopt($ch, CURLOPT_POST, true);
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  7. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  8. if (curl_errno($ch)) {
  9. file_put_contents($log_file, '发生错误: ' . curl_error($ch), FILE_APPEND);
  10. }
  11. $response = curl_exec($ch);
英文:

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

  1. $ch = curl_init();
  2. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "accept: application/json"));
  3. curl_setopt($ch, CURLOPT_URL, $url);
  4. curl_setopt($ch, CURLOPT_POST, true);
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  7. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  8. if (curl_errno($ch)) {
  9. file_put_contents($log_file, 'Error occurred: ' . curl_error($ch), FILE_APPEND);
  10. }
  11. $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:

确定