英文:
Need help on POST request using com.google.api.client.http.HttpRequest
问题
我正在尝试使用com.google.api.client.http.HttpRequest
发送带有请求体数据的POST请求,但是出现了错误com.google.api.client.http.HttpResponseException: 400 Bad Request
。
我需要发送请求"application/x-www-form-urlencoded"
。
以下是我的示例代码:
public static void sendMessage(String url1, String params){
try {
String urlParameters = "{\"name\":\"myname\",\"age\":\"20\"}" ;
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(url1);
HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", postData);
HttpRequest request = requestFactory.buildPostRequest(url, content);
com.google.api.client.http.HttpResponse response = request.execute();
System.out.println("Sent parameters: " + urlParameters + " - Received response: " + response.getStatusMessage());
System.out.println("Response content: " + CharStreams.toString(new InputStreamReader(response.getContent())));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
英文:
I am trying to send the post request using com.google.api.client.http.HttpRequest
with data in request Body but getting error com.google.api.client.http.HttpResponseException: 400 Bad Request
I need to send the request "application/x-www-form-urlencoded"
here is my sample:
public static void sendMessage(String url1, String params){
try {
String urlParameters = "{\"name\":\"myname\",\"age\":\"20\"}" ;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
GenericUrl url = new GenericUrl(url1);
HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", postData);
HttpRequest request = requestFactory.buildPostRequest(url, content);
com.google.api.client.http.HttpResponse response = request.execute();
System.out.println("Sent parameters: " + urlParameters + " - Received response: " + response.getStatusMessage());
System.out.println("Response content: " + CharStreams.toString(new InputStreamReader(response.getContent())));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
答案1
得分: 1
I think your problem is not sending request using this particular class. But encoding parameters themselves. This gives server hard time parsing your request, and in return giving you 400 response.
Your version seems to emulate JSON, but that is not how you encode parameters in HTTP.
The correct method looks like:
name=myname&age=20
Also, remember you need to url encode all data that you are adding to parameters. Otherwise server won't understand your request, and you'll get the same problem again.
Here: https://www.baeldung.com/java-url-encoding-decoding#encode-the-url , you have some good example of how doing URL encoding with Java.
Edit: added example
Following code works:
String urlParameters = "name=Cezary&age=99" ;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
HttpTransport transport = new NetHttpTransport();
HttpRequestFactory requestFactory = transport.createRequestFactory();
GenericUrl url = new GenericUrl("http://localhost:8080/greeting");
HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", postData);
HttpRequest request = requestFactory.buildPostRequest(url, content);
com.google.api.client.http.HttpResponse response = request.execute();
Instead of using ByteArrayContent
and encoding the parameters by yourself, you may use UrlEncodedContent
which will handle the url encoding for you.
Map<String, Object> params = new HashMap<>();
params.put("name", "Cezary");
params.put("age", 99);
HttpContent content = new UrlEncodedContent(params);
The code mentioned above was tested on a server accepting HTTP POST with url encoded parameters within the body. If it still doesn't work for you. You should verify using curl/postman or other utility if that is what your server consumes. If it's not, it's perfectly normal it returns 400 to you.
英文:
I think your problem is not sending request using this particular class. But encoding parameters themselves. This gives server hard time parsing your request, and in return giving you 400 response.
Your version seems to emulate JSON, but that is not how you encode parameters in HTTP.
The correct method looks like:
name=myname&age=20
Also, remember you need to url encode all data that you are adding to parameters. Otherwise server won't understand your request, and you'll get the same problem again.
Here: https://www.baeldung.com/java-url-encoding-decoding#encode-the-url , you have some good example of how doing URL encoding with Java.
Edit: added example
Following code works:
String urlParameters = "name=Cezary&age=99" ;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
HttpTransport transport = new NetHttpTransport();
HttpRequestFactory requestFactory = transport.createRequestFactory();
GenericUrl url = new GenericUrl("http://localhost:8080/greeting");
HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", postData);
HttpRequest request = requestFactory.buildPostRequest(url, content);
com.google.api.client.http.HttpResponse response = request.execute();
Instead of using ByteArrayContent
and encoding the parameters by yourself, you may use UrlEncodedContent
which will handle the url encoding for you.
Map<String, Object> params = new HashMap<>();
params.put("name", "Cezary");
params.put("age", 99);
HttpContent content = new UrlEncodedContent(params);
The code mentioned above was tested on a server accepting HTTP POST with url encoded parameters within the body. If it still doesn't work for you. You should verify using curl/postman or other utility if that is what your server consumes. If it's not, it's perfectly normal it returns 400 to you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论