如何在JAVA中进行HttpPut请求,并将字符串数组作为请求体传递给它?

huangapple go评论87阅读模式
英文:

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[] 数组?

如何在JAVA中进行HttpPut请求,并将字符串数组作为请求体传递给它?

英文:

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"
]

如何在JAVA中进行HttpPut请求,并将字符串数组作为请求体传递给它?

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.

huangapple
  • 本文由 发表于 2020年10月7日 16:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64239860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定