views:

233

answers:

4

I am working on an app where we have to pass specific web api parameters to a web app using HTTP POST. eg: apimethod name parameter1 value parameter2 value So do I use a string or URLEncodedPostData to send that data? It would be good if u help me with a code eg. I am using something like this but it doesnt post the data to the server. Though the response code is ok/200 and I also get get a parsed html response when i read the httpresponse input stream. But the code doesnt post anything. So unable to get the expected response.

             _postData.append("method", "session.getToken");
       _postData.append( "developerKey", "value");
       _postData.append( "clientID", "value");

      _httpConnection = (HttpConnection) Connector.open(URL, Connector.READ_WRITE);
      String encodedData = _postData.toString();
      _httpConnection.setRequestMethod(HttpConnection.POST);
      _httpConnection.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
      _httpConnection.setRequestProperty("Content-Language", "en-US");
      _httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
      _httpConnection.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString());
      os = _httpConnection.openOutputStream();
      os.write(requeststring.getBytes());`
+1  A: 

POSTed parameters are usually sent in the response BODY, which means URL-encoding them is inappropriate. Quote from the HTTP/1.1 protocol:

  Note: The "multipart/form-data" type has been specifically defined
  for carrying form data suitable for processing via the POST
  request method, as described in RFC 1867 [15].
just somebody
+1  A: 

The post method allows you to use pretty arbitrary message bodies — so it is whatever format the server wants.

David Dorward
+1  A: 

The code you posted above looks correct - although you'll want to do a few more things (maybe you did this already but didn't include it in your code):

  1. Close the outputstream once you've written all the bytes to it
  2. Call getResponseCode() on the connection so that it actually sends the request
Marc Novakowski
Can u explain d 2nd point? A code line will be appreciated as well. I did call getResponseCode() like just to get if the connection is ok/200. But how does it actually send the request?
Bohemian
If you are getting a 200 response code, it is sending the request. From what I can tell, it's the call to getResponseCode() that actually triggers the request to be sent - basically you're telling the API that you're done setting up the request and you'd like to send it and get a response back.
Marc Novakowski
Ok I checked it using a var_dump(_post). The app is sending the post data. Just some problem in the api handling the data.Thanks anyways.
Bohemian
A: 

I know this thread is probably dead, however as i'm having a similiar issue, thought i'd comment

surely requeststring is meant to be encodedData?

jpspringall