问题描述
我正在尝试将此 curl 转换为 Google 应用程序脚本,但收到 400 个错误请求
这是 curl 命令
curl --include \
--header "Authorization: Basic YXBpLXVzZXJuYW1lOmFwaS1wYXNzd29yZA==" \
--request POST \
--header "Content-Type: application/json" \
--data-binary " {
\"messages\":[
{
\"source\":\"PHP\",\"body\":\"Jelly liquorice marshmallow candy carrot cake 4Eyffjs1vL.\",\"to\":\"+61411111111\"
},{
\"source\":\"PHP\",\"body\":\"Chocolate bar icing icing oat cake carrot cake jelly cotton MWEvciEPIr.\",\"to\":\"+61422222222\"
}
]
}" \
'https://rest.clicksend.com/v3/sms/send'
解决方法
我相信你的目标如下。
- 您想将问题中的 curl 命令转换为 Google Apps 脚本。
示例脚本:
function myFunction() {
const url = "https://rest.clicksend.com/v3/sms/send";
const params = {
method: "post",headers: { Authorization: "Basic YXBpLXVzZXJuYW1lOmFwaS1wYXNzd29yZA==" },contentType: "application/json",payload: JSON.stringify({
"messages": [
{
"body": "Jelly liquorice marshmallow candy carrot cake 4Eyffjs1vL.","source": "php","to": "+61411111111"
},{
"body": "Chocolate bar icing icing oat cake carrot cake jelly cotton MWEvciEPIr.","to": "+61422222222"
}
]
})
};
const res = UrlFetchApp.fetch(url,params);
console.log(res.getContentText());
}
注意:
- 在这个示例脚本中,它假设您的 curl 命令工作正常。请注意这一点。
- 如果您想查看响应标头,也可以使用
getAllHeaders()
。 Ref