Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restful API codeigniter - JAVA issue

We built our API using Phil Sturgeons cool Restful API framework for codeigniter, which is production ready and is used as part of our mobile apps implementation.

We have an issue when using the API in Java

httpConnection = (HttpConnection) Connector.open(url, Connector.READ, true);
// Set content type by given parameter......
httpConnection.setRequestProperty("Accept", contentType);
httpConnection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/FCLDC-1.0");
httpConnection.setRequestProperty("Content-Type", contentType);
httpConnection.setRequestProperty("TK-API-KEY", UrlFactory.TK_API_KEY);

// httpConnection.setRequestProperty("Model",
// StyleUtil.getDeviceModel());
if (httpConnection.getResponseCode() == 302)
{
  String redirectUrl =  httpConnection.getHeaderField("Location");
  httpConnection = (HttpConnection) Connector.open(redirectUrl, Connector.READ_WRITE, true);
}

if (httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
{
  io = httpConnection.openInputStream();

  int ch;
  while ((ch = io.read()) != -1)
  {
    bo.write(ch);
  }

}

httpConnection.getResponseCode() is unable to get the status code and returns a malformed exception. Our API server is NGINX.

like image 868
user160108 Avatar asked Aug 29 '12 12:08

user160108


1 Answers

MalformedException is thrown in case header fields are not set properly. Please check with different headers and also with different user agents. Trying using httpConnection.setDoOutput(true);

like image 153
geekgugi Avatar answered Nov 14 '22 08:11

geekgugi