Chaitanya Maligi
Chaitanya Maligi

Reputation: 215

While trying to send POST request in HTTPCLIENT -JAVA, getting 400 Bad Request

I am trying to POST a request using JAVA HTTPCLIENT, and while doing so, I am getting 404 Bad Request.

I tried writing the JAVA code in Eclipse and got 404 Bad Request and tried sending the request through POSTMAN and received HTTP Status 500

package com.apex.customer.service;

public class CustServicePostTest {

    public static void main(String[] args) throws ClientProtocolException, IOException {

        String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/102";
        //create the http client
        HttpClient client = HttpClientBuilder.create().build();
        //create the post message
        HttpPost post = new HttpPost(url);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("ID", "102"));
        urlParameters.add(new BasicNameValuePair("FIRSTNAME", "Apex"));
        urlParameters.add(new BasicNameValuePair("LASTNAME", "Consultancy"));
        urlParameters.add(new BasicNameValuePair("STREET", "Shell Blvd"));
        urlParameters.add(new BasicNameValuePair("CITY", "Fremont"));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);

        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println("Parameters : " + urlParameters);
        System.out.println("Response Code: " + response);
        System.out.println(response.getStatusLine().getReasonPhrase());

    }

}

I am looking for 200 OK request.

Upvotes: 1

Views: 2489

Answers (1)

Marcio Jasinski
Marcio Jasinski

Reputation: 1479

The issue here is due few mistakes:

  • First is related to the input format. The code you're using tries to map key and values, but as I could see from this guide, it expects a XML format in a plain text as input.
  • The second mistake is that you are trying to post over an existing ID. In this case, to create a resource you should use http://www.thomas-bayer.com/sqlrest/CUSTOMER/

So in this case in order to make it work, try something like this:

        String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/";
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);

        String xml = "<resource>";
        xml += "<ID>102</ID>";
        xml += "<FIRSTNAME>Apex</FIRSTNAME>";
        xml += "<LASTNAME>Consultancy</LASTNAME>";
        xml += "<STREET>Shell Blvd</STREET>";
        xml += "<CITY>Fremont</CITY>";
        xml += "</resource>";

        post.setEntity(new StringEntity(xml));
        HttpResponse response = client.execute(post);

        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println("Response Code: " + response);
        System.out.println(response.getStatusLine().getReasonPhrase());

It is also very useful to learn another way to test it with tools like curl command line utility. For example you can POST a product like this:

curl -X POST  http://www.thomas-bayer.com/sqlrest/PRODUCT/ -d '<resource><ID>103</ID><NAME>X</NAME><PRICE>2.2</PRICE></resource>'

Once you solve this, it will be important to get used with HTTP codes. For example a 500 error means something wrong on the server side while a 404 usually means that you're hitting an invalid endpoint (it does not exists).

Finally, I'll not discuss why are you using this project to send HTTP requests to a server - but keep in mind that this is not a very common way to go. Currently the REST with JSON would be much more interesting and enjoyable :) In case you're interested on it, take a look on Spring Boot REST

Upvotes: 1

Related Questions