javagoogle-api-java-clientgoogle-url-shortenergoogle-http-client

Need help on POST request using com.google.api.client.http.HttpRequest


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);
        }
    }

Solution

  • 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.