无法在Google Apps脚本上发送发布请求 [英] Unable to send Post Request on Google Apps Script

查看:114
本文介绍了无法在Google Apps脚本上发送发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送发布请求以验证交易。我使用



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
},
有效载荷:{
SECKEY: FLWSECK-e6db11d1f8a6208de8cb2f94e293450e-X,
txref: MC-1520443531487
}
};

const响应= UrlFetchApp.fetch(URL,options);

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

应用脚本错误响应:



的请求失败 https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify
返回了代码400。



服务器响应被截断:SyntaxError:意外的令牌S
 
 解析
(/app/node_modules/body-parser/lib/types/json.js:83:15)
 在/ app / node_mod ...的
 (使用MutantHttpExceptions选项检查
的完整响应)(第299行,文件代码)


我在做什么错或如何在Google Apps脚本中发送帖子请求?

解决方案

UrlFetchApp.fetch()函数有一个不寻常的怪癖,因为您必须使用 contentType 属性来设置有效内容的内容类型。出于某些原因,直接设置标头似乎无效。



此外,您还需要 JSON.stringify()您的有效载荷。



因此,重写您的发帖请求,如下所示:

  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: [您的秘密密钥],
txref: [您的TXREF代码]
})
};

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

请注意,我省略了带有占位符文本的键和代码。您真的不应该分享这种敏感信息。如果可能的话,我强烈建议您撤销这些密钥,并让服务供应商发行新的密钥。


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?

解决方案

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.

这篇关于无法在Google Apps脚本上发送发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆