需要将此 curl 转换为 Google 应用程序脚本 Urlfetch

Need to convert this curl to Google apps script Urlfetch

我正在尝试将此 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.",
          "source": "php",
          "to": "+61422222222"
        }
      ]
    })
  };
  const res = UrlFetchApp.fetch(url, params);
  console.log(res.getContentText());
}

注:

  • 在此示例脚本中,它假设您的 curl 命令工作正常。请注意这一点。
  • 如果要查看响应头,也可以使用getAllHeaders()Ref

参考: