简体   繁体   English

使用Java中的JSON到Bitbucket服务器的HTTP POST返回400作为响应代码

[英]HTTP POST using JSON in java to bitbucket server returns 400 as response code

I am trying to do a POST request for adding comment field to bitbucket server using Java. 我正在尝试执行POST请求,以使用Java将注释字段添加到bitbucket服务器。 I am always getting Response code as 400. 我总是收到响应代码为400。

String data=  "text=" + params;

String restUrl = m_ServerName + "/" + restUrl;
HttpPost post = new HttpPost(restUrl);
post.addHeader("Authorization","Basic " + m_EncodedLogin);  
post.addHeader("Content-type", "application/json");
post.addHeader("Accept", "application/json");

post.setEntity(new StringEntity(data, Consts.UTF_8));
CloseableHttpResponse response = m_HttpClient.execute(post);
System.out.println(response.getStatusLine().getStatusCode());

I found the solution, the issue is with the string data variable I was forming. 我找到了解决方案,问题出在我正在形成的字符串数据变量上。 Working code is as follows:- 工作代码如下:

String data=  "{\"text\"=" + \"params\" + "}";

String restUrl = m_ServerName + "/" + restUrl;
HttpPost post = new HttpPost(restUrl);
post.addHeader("Authorization","Basic " + m_EncodedLogin);  
post.addHeader("Content-type", "application/json");
post.addHeader("Accept", "application/json");

post.setEntity(new StringEntity(data, Consts.UTF_8));
CloseableHttpResponse response = m_HttpClient.execute(post);
System.out.println(response.getStatusLine().getStatusCode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM