英文:
How to make HttpPut request with string array as body to it in JAVA?
问题
以下是我翻译好的内容:
我有一个需求,需要调用一个接受一个参数 Body(类型为 array[string])的 rest api put 方法,该参数需要传递到“BODY”,而不是查询参数。
以下是该参数接受的值:
[
"string"
]
这是我尝试进行调用的步骤:
创建了一个 OAuth 消费者对象,用于对请求进行签名,以及一个 HttpClient 对象,用于执行请求。
consumer = new CommonsHttpOAuthConsumer("abc", "def");
requestConfig = RequestConfig.custom().setConnectTimeout(300 * 1000).build();
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
创建带有 JSON 的 put 请求
URL url = new URL("http://www.example.com/resource");
String[] dfNames = {};
dfNames[0] = "test";
putreq = new HttpPut(url);
putreq.setHeader("Id", "xyz");
StringEntity input = new StringEntity(dfNames); // 此处出现编译错误
input.setContentType("application/json");
putreq.setEntity(input);
执行请求以获取响应代码
updateresponse = httpClient.execute(putreq);
int updatehttpResponseCode = updateresponse.getStatusLine().getStatusCode();
System.out.println("post Response Code :: " + updatehttpResponseCode);
if (updatehttpResponseCode == 200) {
System.out.println("PuT request worked");
} else {
System.out.println("PuT request not worked");
}
输出:
在运行此代码时,我遇到了编译错误,因为 StringEntity 类不能接受字符串数组。是否有其他类可以接受 string[] 数组?
英文:
I have a requirement to call a rest api put method which accepts one parameter Body(of type arrray[string]) which is to be passed to "BODY" not query parameter.
Below is the value that this parameter accepts:
[
"string"
]
These are the steps that I tried to make the call:
created an oauth consumer object that is used to sign the request and httpclient object to execute the request
consumer = new CommonsHttpOAuthConsumer("abc", "def");
requestConfig = RequestConfig.custom().setConnectTimeout(300 * 1000).build();
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
Creating the put request with json
URL url = new URL("http://www.example.com/resource");
String[] dfNames = {};
dfNames[0] = "test";
putreq = new HttpPut(url);
putreq.setHeader("Id","xyz");
StringEntity input = new StringEntity(dfNames); //Getting compilation error
input.setContentType("application/json");
putreq.setEntity(input);
Executing the request to get the response code
updateresponse = httpClient.execute(putreq);
int updatehttpResponseCode = updateresponse.getStatusLine().getStatusCode();
System.out.println("post Response Code :: " + updatehttpResponseCode);
if (updatehttpResponseCode == 200)
{
System.out.println("PuT request worked);
}
else
{
System.out.println("PuT request not worked");
}
OUTPUT:
I am getting a compilation error when running this since the string entity class cannot accept string array. Is there any other class which will accept the string[] array?
答案1
得分: 0
根据评论讨论,HTTP PUT 请求体只是一个字符串,所以只需将数组转换为字符串(使用 Arrays.toString() 或其他方法),然后使用它。
英文:
As discussed in the comments, the HTTP PUT body is just a string, so just convert the array to String (with Arrays.toString() or any other way) and use it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论