Unable to send Post Request on Google Apps Script

丶灬走出姿态 提交于 2019-12-13 03:57:16

问题


I am trying to send a post request to verify a transaction. I sent the same post request with Postman and I got a response as shown below:

Apps Script Code.gs:

function postRequest() {
    var url = "https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify";
    var options = {
        "method": "post",
        "headers": {
            "Content-Type": "application/json"
        },
        "payload": {
            "SECKEY": "FLWSECK-e6db11d1f8a6208de8cb2f94e293450e-X",
            "txref": "MC-1520443531487"
        }
    };

    const response = UrlFetchApp.fetch(url, options);

    Logger.log(JSON.stringify(response));
}

Apps Script Error Response:

Request failed for https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify returned code 400.

Truncated server response: SyntaxError: Unexpected token S
   at parse (/app/node_modules/body-parser/lib/types/json.js:83:15)
   at /app/node_mod... (use muteHttpExceptions option to examine full response) (line 299, file "Code")

What am I doing wrong or How do I send a post request in Google Apps Script?


回答1:


The UrlFetchApp.fetch() function has an unusual quirk in that you have to use the contentType property to set the content type of the payload. For some reason, setting the header directly doesn't seem to work.

In addition you need to JSON.stringify() your payload.

So rewrite your post request as follows:

function verify() {
    var url = "https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify";

    var options = {
        "method":"POST",
        "contentType":"application/json",
        "payload":JSON.stringify({
            "SECKEY":"[YOUR-SECRET-KEY]",
            "txref":"[YOUR-TXREF-CODE]"
        })
    };

    return JSON.parse(UrlFetchApp.fetch(url, options));
}

Note that I omitted the key and code with placeholder text. You really shouldn't share that kind of sensitive information. If possible I strongly recommend revoking those keys and have the service vendor issue new ones.




回答2:


This is not a problem with Google Apps Script but the flutterwave server is returning a 400 error code.

You can check this by adding "muteHttpExceptions": true to the options object and then logging the response in the console.

function postRequest1() {
    var url = "https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify";
    var options = {
        "method": "post",
        "headers": {
            "Content-Type": "application/json"
        },
        "payload": {
            "SECKEY": "FLWSECK-e6db11d1f8a6208de8cb2f94e293450e-X",
            "txref": "MC-1520443531487"
        },
        "muteHttpExceptions": true
    };

    const response = UrlFetchApp.fetch(url, options);
Logger.log(response.getResponseCode())
Logger.log(response.getContentText())
}



回答3:


Thanks to Dimu Designs for explaining a quirk of UrlFetchApp.fetch()

All I did to make it work was to change Content-Type to contentType

function postRequest() {
    var url = "https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify";
    var options = {
        "method": "post",
        "headers": {
            "contentType": "application/json" // <--  HERE
        },
        "payload": {
            "SECKEY": "FLWSECK-e6db11d1f8a6208de8cb2f94e293450e-X",
            "txref": "MC-1520443531487"
        },
        "muteHttpExceptions": true
    };

    const response = UrlFetchApp.fetch(url, options);

    Logger.log(response.getContentText());
    Logger.log('Response Code: '+response.getResponseCode())
}

On the other hand "muteHttpExceptions": true is there so that I will handle the Http exceptions myself.



来源:https://stackoverflow.com/questions/55475286/unable-to-send-post-request-on-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!