0

I am trying to post xml data to API using HTTP post method with credentials but a getting HTTP/1.1 400 Bad Request error .. Can anyone pl help me out ....

Here is my sample code:

BufferedReader br = new BufferedReader(new FileReader(new File("Data.xml")));

        StringBuilder sb = new StringBuilder();

        while((line=br.readLine())!= null){
            sb.append(line.trim());
            }
        System.out.println("xml:  "+sb);



        params=sb.toString();
        HttpPost request = new HttpPost("*****************url***************");
        String urlaparam=URLEncoder.encode("importFormatCode:1&data:"+params,"UTF-8");
        String userCredentials = "****:******";
        byte[] auth = Base64.encodeBase64(userCredentials.getBytes());

        StringEntity entity=new StringEntity(urlaparam);
        request.addHeader("Content-type","application/x-www-form-urlencoded");
        request.addHeader("Accept", "application/xml");
        request.addHeader("Accept-Language", "en-US,en;q=0.5");
        request.addHeader("Authorization", "Basic " + new String(auth));


        request.setEntity(entity);
        HttpResponse response = httpClient.execute(request);    

       System.out.println(response.getStatusLine());
       System.out.println(request);
}
    catch(Exception e)
{
}
3
  • I think, your parameter format is wrong. The key-value separator should be "=", not ":". You can use org.apache.http.client.utils.URLEncodedUtils.format(...) to encode your parameters. Commented Jun 2, 2014 at 10:35
  • can u pl explain briefly abt encoding Commented Jun 2, 2014 at 10:50
  • See w3.org/TR/html401/interact/forms.html#h-17.13.4.1 Commented Jun 2, 2014 at 11:47

1 Answer 1

1

First of all, your form parameters are not encoded correctly. You are using colon (:) to separate keys from their values, but instead, the equal sign (=) must be used:

  • Wrong: "importFormatCode:1&data:" + params
  • Correct: "importFormatCode=1&data=" + params

(See also W3C.org - Forms in HTML Documents - application/x-www-form-urlencoded)

Apart from that, you must not URL-encode the entire string but only the keys and the values. Otherwise you'll also encode the separator characters = and &!

The easiest way is to use the existing utility class org.apache.http.client.utils.URLEncodedUtils (assuming that you're using Apache HTTP Components):

String xmlData = // your xml data from somewhere

List<NameValuePair> params = Arrays.asList(
    new BasicNameValuePair("importFormatCode", "1"),
    new BasicNameValuePair("data", xmlData)
);
String body = URLEncodedUtils.format(params, encoding); // use encoding of request

StringEntity entity = new StringEntity(body);
// rest of your code
Sign up to request clarification or add additional context in comments.

5 Comments

its fine .. thanks for your valuable reply but pl tell me more about encoding that you have done ...string body = URLEncodedUtils.format(params, encoding);
@user3698939 What do you mean? I'm just calling the method of the utility class.
@user3698939 If you are interested in the encoding itself, please have a look at the W3C link I've posted.
Are you sure, the request is application/x-www-form-urlencoded? Usually, data like XML is NOT sent via HTTP that way.
Then you should better check the server API documentation; maybe something else is wrong (XML syntax, ...). Also inspect the error message that might come together with the error response. And do some tests using a REST-Client (e.g. Browser Plugin) to ensure the API works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.