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

Delphi TRESTClient调用Jira API提交评论报错,如何正确传递--data?

Jira API 评论更新的Delphi TRESTClient问题

我尝试通过Jira官方文档提供的curl示例更新Jira评论,但使用Delphi的TRESTClient组件时,始终收到HTTP 415或400错误。另外使用rmGet方法和PAT认证查询数据的功能是正常的。

官方curl示例代码

curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://kelpie9:8081/rest/api/2/issue/QA-31/comment

{
    "body": "This is a comment regarding the quality of the response."
}

我编写的Delphi代码

request.Method := TRESTRequestMethod.rmPost;
request.Params.AddItem('Content-Type','application/json',   TRESTRequestParameterKind.pkHTTPHEADER);
request.Resource := '/jira/rest/api/2/issue/QA-31/comment';
request.AddBody('{"This is a rest api comment update." }', ctAPPLICATION_JSON);
request.Params.AddItem('body','{"This is a rest api comment update." }' , TRESTRequestParameterKind.pkGETorPOST);
request.Params.AddItem('data','{"body": "This." }',TRESTRequestParameterKind.pkGETorPOST);
request.Body.Add('{"body": "This is a rest api comment update." }', ctAPPLICATION_JSON);
request.Execute;

问题

想请教curl中的--data参数对应以下哪种正确的传递方式?

  • a) request.AddBody
  • b) request.Body.Add
  • c) request.Params.AddItem

解答

curl的--data参数是将指定的JSON数据作为HTTP请求体发送给服务器,对应选项a和b,但你的代码存在多个错误导致请求失败:

  1. 重复设置请求体:你同时调用了AddBody、Body.Add,还通过Params.AddItem添加了body和data参数,这会导致请求体内容混乱,或者服务器无法识别正确的参数格式。
  2. JSON格式错误:request.AddBody('{"This is a rest api comment update." }', ...)中的JSON是无效的,Jira API要求的是包含body键的键值对,比如{"body": "你的评论内容"}。
  3. 错误使用Params传递参数:Jira的这个评论更新API要求JSON放在请求体中,不是表单参数,所以不能用Params.AddItem来添加body或data。

正确的代码写法

request.Method := TRESTRequestMethod.rmPost;
// 确保Content-Type头正确(如果组件自动设置可以省略,但显式设置更稳妥)
request.Params.AddItem('Content-Type','application/json', TRESTRequestParameterKind.pkHTTPHEADER);
request.Resource := '/jira/rest/api/2/issue/QA-31/comment';
// 只需要一次设置请求体,使用正确的JSON格式
request.AddBody('{"body": "This is a rest api comment update."}', ctAPPLICATION_JSON);
// 移除所有多余的Params.AddItem和Body.Add调用
request.Execute;

总结:curl的--data对应request.AddBody或request.Body.Add(二者都是操作请求体,选a或b都可以,但不要混用),绝对不能用选项c。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 08:35:24