You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Groovy调用短信面板API时遭遇401 ERROR问题求助

排查Jira后置函数调用短信API的401错误

我是API新手,公司采购了一款短信面板,需要通过Jira后置函数向客户发送短信。尝试调用该面板的API实现短信发送时,一直返回401 ERROR,不清楚问题所在。

对方提供的API请求示例(Postman导出格式):

{
  "name": "Send",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [ ""  ],
        "type": "text/javascript",
        "packages": {}
      }
    }
  ],
  "protocolProfileBehavior": {
    "followRedirects": true,
    "disableUrlEncoding": false,
    "disableCookies": false
  },
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [{
          "key": "token",
          "value": "{{authToken}}",
          "type": "string"
        }]
    },
    "method": "POST",
    "header": [{
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Authorization",
        "value": "Bearer"
      },
      {"key": "",
        "value": ""}
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n\t{\n\t\t\"sender\": \"982000xxx\",\n\t\t\"recipient\": \"98xxxx\",\n\t\t\"body\": \"string\",\n\t\t\"customerId\": \"string\"\n\t}\n]",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://www.test.com/panel/send",
      "protocol": "https",
      "host": [
        "www",
        "test",
        "com"
      ],
      "path": [
        "panel",
        "send"
      ]
    }
  },
  "response": []
}

我编写的Groovy代码:

def httpBuilder = new HTTPBuilder("https://www.test.com")
httpBuilder.headers['Content-Type']="application/json"
httpBuilder.headers['Authorization']=( "Bearer " + "accesstoken")
httpBuilder.request(POST,JSON) {
    uri.path="/panel/send"
   body= [sender: "XXXXX",recipient :"XXXXX",body:"Test",customerId :"1"]
 
    response.success = { resp, json ->
                log.debug  'Successful is ' +resp.status
            }
            response.failure = { resp, json ->
                log.debug  'Reader is '+resp.status
            }
         }

可能的401错误原因:

  • 请求Body格式不匹配:对方示例中请求Body是JSON数组(外层包裹[]),但你的Groovy代码中body是单个Map对象,最终会被序列化为单个JSON对象{...},部分API会因为请求结构不符合要求直接返回401(而非400格式错误)。
  • Bearer Token有效性问题:检查accesstoken是否正确、未过期,确认Authorization头的格式:Bearer 与token之间只能有一个空格,token本身不能包含多余的空格或换行符。
  • Jira环境限制:Jira服务器可能存在网络代理、防火墙规则,导致请求无法正确到达短信API服务器,或者请求中的Authorization头被拦截篡改,最终返回401。
  • 授权方式冲突:对方示例同时在request.auth和请求头中配置了Bearer授权,虽然大部分API以请求头为准,但可以确认该短信API是否要求必须通过特定方式传递token。

内容的提问来源于stack exchange,提问作者Dr Sima

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:14:55