Search code examples
javapythonapachedjango-piston

Django-Piston and Python Client Vs Java Client


I've built a web service using Django-Piston that allows for POST and GET requests. As part of testing I wrote a quick Python script. Using the script I can successfully do both types of requests; however, when a client written in Java attempts to do a POST I get an error: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"

My understanding is that http request message that is generated by any language has to be the same. In other words, if I test my web service using a python client and it works then it should work for all other languages that have a http library.

Here is the python code for the POST:

 import urllib, urllib2

 data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
 url = 'http://xxx/api/service/'
 req = urllib2.Request(url, data)

 print urllib2.urlopen(req).read() 

and here is the Java code:

HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));

I'm starting to think that this is an apache configuration problem. I put in some debugging statements at the start of my method for POST and I'm not hitting them at all. This means that something is wrong with the urls.py file (which I doubt because it works in python) or something is weird with apache.

Please help. Thanks in advance.


Solution

  • A little search would help you a lot. This was the first Google result.

    http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/

    The reason for the 400 errors is that ExtJS is appending a charset onto the Content-Type field, which causes piston to not interpret the Content-Type correcly. There is an open issue for it at http://bitbucket.org/jespern/django-piston/issue/121/content-type-is-not-being-split-against. I was able to get the example working after I applied the patch and did an easy_install.

    This is the second Google response.

    https://bitbucket.org/jespern/django-piston/issue/99/bad-request-and-content-type-with-fix