Arindam Mukherjee
Arindam Mukherjee

Reputation: 2285

Getting 411 response code while getting JSON Response

In my application, I am trying get the response using POST request. The response server is sending me in Json format. But after adding the properties, it is returning me the response code as 411 (i.e issue with content length). I have already added the content length. Then where is the issue i am not getting. Here is my code:

String url = "https://xxx:8243/people/v3";
STRURL = url + HttpComm.getConnectionString().trim();

StringBuffer postData = new StringBuffer();
HttpConnection httpConnection = null;
try {
    httpConnection = (HttpConnection) Connector.open(STRURL);
} catch (IOException e1) {
    e1.printStackTrace();
};

try {
    httpConnection.setRequestMethod("POST");
    postData.append("?username="+user);
    postData.append("&password="+password);
    String encodedData = postData.toString();
    byte[] postDataByte = postData.toString().getBytes("UTF-8");
    httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
    httpConnection.setRequestProperty("Content-Type","application/json");
    httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
    OutputStream out = httpConnection.openOutputStream();
    DataOutputStream dos = new DataOutputStream(out);
    out.write(postData.toString().getBytes());
    out.flush();
    int statusCode = httpConnection.getResponseCode();
    Logger.out("HttpComm", "status code:::::::   "+statusCode);
    if (statusCode != HttpConnection.HTTP_OK)
    {

    }

Updated Code :

HttpConnection httpConnection = null;
    try {
        httpConnection = (HttpConnection) Connector.open(STRURL);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    };
            try {
                httpConnection.setRequestMethod("POST");
                URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
                 postData.append("username", user);
                 postData.append("password", password);
                 byte[] postDataByte = postData.getBytes();
                  httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
                   httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                   httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
                   OutputStream out = httpConnection.openOutputStream(); 
                   out.write(postDataByte);
                   out.flush();

                   int statusCode = httpConnection.getResponseCode();
                   Logger.out("HttpComm", "status code:::::::   "+statusCode);

Upvotes: 2

Views: 1211

Answers (1)

Nate
Nate

Reputation: 31045

There are a few things that don't look quite right here. I would recommend trying this:

 httpConnection.setRequestMethod("POST");

 URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
 postData.append("username", user);
 postData.append("password", password);
 byte[] postDataByte = postData.getBytes();

 httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));

 OutputStream out = httpConnection.openOutputStream(); 
 DataOutputStream dos = new DataOutputStream(out);
 out.write(postDataByte);
 out.flush();

 int statusCode = httpConnection.getResponseCode();
 Logger.out("HttpComm", "status code:::::::   "+statusCode);
 if (statusCode != HttpConnection.HTTP_OK)

What I changed:

  1. As @samlewis said, the code was creating a variable to hold the post data bytes, but then was not using it when it called out.write().

  2. The code set the content type to JSON, but it was not sending JSON. The request was simply two parameters. The response may be JSON, but you don't specify that in the request's Content-Type parameter.

  3. The username/password parameters were encoded just using strings. Normally, it's best to use the URLEncodedPostData class to hold your POST parameters.

  4. If you are going to use strings, I think it was still incorrect to add a ? to the front of the username parameter. If you want to encode parameters in a GET URL, then you use https://xxx:8243/people/v3?username=user&password=password. But, this code was using POST, not GET.

  5. There was also an unused encodedData variable.

Upvotes: 1

Related Questions