EdgeDev
EdgeDev

Reputation: 2486

Unable to send Post Request on Google Apps Script

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:

enter image description here enter image description here

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?

Upvotes: 0

Views: 3514

Answers (3)

EdgeDev
EdgeDev

Reputation: 2486

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.

Upvotes: 0

TheAddonDepot
TheAddonDepot

Reputation: 8964

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.

Upvotes: 3

Amit Agarwal
Amit Agarwal

Reputation: 11268

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

Upvotes: 1

Related Questions