Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Getting 411 response code while getting JSON Response

Hi,

 

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) {
			// TODO Auto-generated catch block
			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)
					    {
					    	
					    }

 Please help. 

Thanks.

Please use plain text.
Developer
simon_hain
Posts: 16,136
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Getting 411 response code while getting JSON Response

you set postDataByte.length as the header, but send postData.toString().getBytes().
My first try would be to send the same array that you get the length from.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: Getting 411 response code while getting JSON Response

Thanks Simon. Added the content-length like below :

 

httpConnection.setRequestProperty("Content-Length", String.valueOf(postData.toString().getBytes()));
httpConnection.setRequestProperty("Content-Length", postData.toString()); httpConnection.setRequestProperty("Content-Length", String.valueOf(postData.toString()));
 

 

But getting the response code 411.

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 411 response code while getting JSON Response

[ Edited ]

There is some dubious code in your method.  We can't correct the code, you will have to review the Server API and learn how to code http POSTs to get it correct.  But let me point out where I think your problems might be.

 

Here is the original code:

 

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

 

Going through the dubious lines:

 

1) postData.append("?username="+user);
 postData.append("&password="+password);

 

The '?' and '&' separators that you have used here are normally used as part of the URL when you are doing a GET.  They look wrong as part of parameters to be used for a POST.  I would review the Server specifications to make sure that you are in fact requesting things correctly.

 

2) byte[] postDataByte = postData.toString().getBytes("UTF-8");

This is correct and will give you the correct bytes, assuming that your server is expecting to receive UTF8.  You need to confirm this.

 

3) httpConnection.setRequestProperty("Content-Type","application/json");

You are NOT sending JSON formatted data so this is not the correct content type.  Either change your content or change the type to match what your server actually expects. 

 

4) httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));

I recommend that you use the HttpProtocolConstants to specify the Header values, then you will not have issues with case or spelling the Header incorrectly.  In this case you would specify:

httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postDataByte));

 

5) out.write(postData.toString().getBytes());

You should replace this line with

out.write(postDataByte);

 

Please review again what SImon said.  You do not seem to have understood it, but he was actually suggesting you make this change

 

6) out.flush();

After this line, thought not strictly necessary, you should code:

out.close();

In addition, I would add

out = null;

 

7) Remember that you only add a header once

In your last post you seem to have added 3 different Content Length headers, one of which was actually the data.  That makes no sense.  I'm guessing you are not familiar with using POST and GET http requests.  I recommend that you review some documentation or talk to someone who has done it before, otherwise I suspect you will continue to try things that are not actually valid and waste a lot of time. 

 

If you correct all of this, and are still getting a 411, then talk to your Server folks and get them to tell you what is actually wrong with your request.

Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: Getting 411 response code while getting JSON Response

Thanks Peter for this beatiful explanation. First of all I want to say, i am trying to develop one app based on the Yookos API. You can find here: api.yookos.com. Now, this APIs are basically like Facebook API. But they are pretty new and even not mature. So i don't have any proper documentation and guide. Even i cannot ask any one about what parameters I need to pass and what should be the correct way. Although I have worked with JSON in android and also with http, but here I am perplexed as i don't have any document for reference.   

I'm following this url basically : 

 

https://api.yookos.com:8243/places/v3?username=[yookos-username]&password=[yookos-password] 

 

So i need to pass the username and password and server will return me a JSON format response. Now I am passing the authentication details like posted in the above code. 

Now I need to ask some queries : 

 

1. Do I need to do Post request or Get request.

2. I did not added the three content-length parameters one by one. Not at a time.  

 

 

I will try with your suggestions and will let you know. 

 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 411 response code while getting JSON Response

Having had a quick review of the API documentation, to log in and get a user token, which is what you appear to be trying to do, you should use POST. 

 

Reviewing the API suggests that you are not using the correct API, as documented here:

http://api.yookos.com/documentation/api/v3/rest/index.html#authentication

I suggest you review the documentation again.

 

Amongst things that appear to be wrong are:

 

1) You are not using the correct URL

 

2) the payload has this format:

grant_type=password&username=<username>&password=<password>&scope=PRODUCTION

 

3) The Authorization header is different

 

Since you have worked in Android, you could try to construct the processing in Android.  Once you have figured it out in Android, it should be the same in BlackBerry. 

 

Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: Getting 411 response code while getting JSON Response

Sorry the authentication part i already completed with post request. I have got the access token and all. So here i want to get the friends list or a particular friend from the user. When i am hitting the same url in the browser with user name and password, it is returning me the JSON response. 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 411 response code while getting JSON Response

"Sorry the authentication part i already completed with post request"

This was not clear to me.  Can you point me at the part of the documentation that you are attempting to follow?

 

"When i am hitting the same url in the browser with user name and password"

 

How exactly do you do this?  What happens when you use the Browser?

 

This should give you a clue as to the problem you are facing.  In the Browser, assuming you are just typing in the request, you are using a GET and suffixing the request with the user name and password.  If you are prompted with user name and password, then you have not supplied the token that you get from the authentication and so are being asked to log in again. 

 

Have you got this part working in Android? 

Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: Getting 411 response code while getting JSON Response

First of all i have type that URL in desktop browser and got the below response (JSON) string..

http://pastie.org/8061788

 

Now How i got the URL? If you checked this API, here you can understand :

 

http://api.yookos.com/documentation/api/v3/rest/PersonEntity.html

 

And one more point, after authentication of the user, i got this values :

 

{"token_type":"bearer","expires_in":7516673,"refresh_token":"e43a1c7e4abfceb20588485d87fc8a6","access_token":"81cc46ea257eb3326905751da32476"}

 

Now come to my code:  

 

I have change my code like below:

 

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

 But still i am getting status code as 411.

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 411 response code while getting JSON Response

Sorry, I don't have time to review the API in detail to understand what you are doing wrong here and I suspect you need some help from someone who is familiar with the API you are trying to use.

 

Looking through the link you supplied did not actually help much I'll be honest, there are a wide variety of possible things that you could do there, some are GETs, some a POSTs.  None of them seemed to require the authentication header you are attaching and it looks to me if you are POST ing you should use JSON format parameters.  So from my brief look I am not understanding which specific request you are trying to do, but nor do I understand the API in enough detail to really comment, even if I did know exactly what you were trying to do.

 

As previously noted, this does not seem to be a BlackBerry issue, so I would look round for some other forums and see if you can find someone else, even on another platform, who has used this API. 

 

Sorry I can't help more.

Please use plain text.