Re: POSTing JSON to Tastypie from Android
Thanks, I got it to work after much of trial and error.
Here is the gist - https://gist.github.com/ratpik/5232763
Thanks,
Pratik
On Sunday, March 24, 2013 8:46:09 PM UTC+5:30, ke1g wrote:
There are a couple of standard ways, IIRC, to encode post parameters, but neither of them is JSON in the body. Of course you can do as you want, and android apparently does, but other software is unlikely to recognize it.
Again, from memory, the two methods are using url encoding, and multi-part form data. The latter is particularly useful for large binary blobs (like uploading files), but it looks as though the first method would work for you. You'll have to dig down into the android API to see how to do it there, but it probably involves a particular value in the content-type header.
BillOn Sat, Mar 23, 2013 at 9:52 AM, Pratik Mandrekar <pratikm...@gmail.com> wrote:
Thanks for the pdb tip Bill!I tried pdb at the point where the request enters Tastypie (based on http://stackoverflow.com/questions/13006901/tastypie- ). What I see is that the request.POST and request.raw_post_data are both empty. I posted from Android client to a posttestserver and I can see that all the headers I set and the request body are set.obj-create-method-not-being- called However it does say that there are no POST parameters. Here is the logged request - http://posttestserver.com/data/2013/03/23/06.42. . Any idea what that means? Note that I had to remove the authorization header to post to this server.591241431084 Also I'm not sure now where else to start the trace from. Is there another place I can pdb trace my HTTP request when it enters the app? Is it possible some data gets stripped off the original request somewhere?Thanks.Pratik
On Saturday, March 23, 2013 5:50:28 PM UTC+5:30, ke1g wrote:Still, if you have a break point where the request comes in to the view, you can inspect the POST data to see how it differs from what you expect. That might inform you as to what you might do differently in your JavaScript, whether you need some additional or different encoding or quoting of the data, whether you need to be more explicit about the content-type header (you may have to look at the WSGI request object itself), etc. Or you may discover that you need to breakpoint at the WSGI application script level to see what you need to see.
Since nobody has jumped in with "I recognize that problem", inspecting your data is probably a good use of your time. pdb is not hard to use, as long as you are using runserver (and you can almost always arrange to explore a problem under runserver) because there has to be a console for pdb to type on, and on which you can type commands. The first breakpoint is the only django specific trick. Put
import pdb; pdb.set_trace()
where you want to stop. You can use the b command to set additional breakpoints later, but that can be confusing due to threading. pdb is documented in the library reference for your python version at python.org. Read up on the p, pp, u, d, c, n, s, r, !, and q, commands, probably in that order, and you will find that you usually only use the first 5 of them. (There are more, for when you want to be a pdb expert.) And remember to start with a p or pp when you want to see the value of an expression, lest your expression be interpreted as one of the other commands.
BillOn Sat, Mar 23, 2013 at 5:19 AM, Pratik Mandrekar <pratikm...@gmail.com> wrote:
The issue is with setting data in the http post request. I have tried it with curl and the web client and it works. CSRF is not the issue, requests work fine without the csrftoken outside of the android client.Pratik
On Saturday, March 23, 2013 12:01:21 AM UTC+5:30, ke1g wrote:Have you tried a breakpoint in the view? Might it be a CSRF problem?On Fri, Mar 22, 2013 at 2:22 PM, Pratik Mandrekar <pratikm...@gmail.com> wrote:
Hello,I'm unable to get the POST json to tastypie from an android http client to work.I have tried with HttpURLConnectionurlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Content-Type", "application/json"); byte [] encoded = Base64.encode((username+":"+password).getBytes("UTF-8"), Base64.DEFAULT); urlConnection.setRequestProperty("Authorization", "Basic "+ new String(encoded, "UTF-8")); JSONObject jsonObject = new JSONObject();jsonObject.put("key1", "value1");jsonObject.put("key2", "value2");outputStreamWriter = urlConnection.getOutputStream(); outputStreamWriter.write(jsonObject.toString().getBytes()); outputStreamWriter.flush();And I have tried with Apache HttpClientHttpClient client=new DefaultHttpClient();HttpPost post = new HttpPost(url);post.setHeader("accept", "application/json");post.addHeader("Content-Type", "application/json");post.addHeader("Authorization", "Basic "+ new String(encoded, "UTF-8")); ArrayList localArrayList = new ArrayList();localArrayList.add(new BasicNameValuePair("json",jsonObject.toString())); lotlisting.setEntity(new UrlEncodedFormEntity(localArrayList)); String str = EntityUtils.toString(localDefaultHttpClient.execute(lotlisti ng).getEntity()); StringEntity se = new StringEntity( jsonObject.toString());se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));post.setEntity(se);HttpResponse response = client.execute(post);I hit the same issue with both of them i.e the POST data as seen as Querydict in Django, does not have any data. This makes it an invalid json and it throws a JSON could not be decoded error.I have tried playing with all the parameters with little luck. Note that get works perfectly, even with parameters.Has anyone been successfully able to post json from an android client to django/tastypie? If yes, could you please share what worked for you?Thanks.To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.cPratik--
You received this message because you are subscribed to the Google Groups "Django users" group.om .
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en .
For more options, visit https://groups.google.com/groups/opt_out .
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com .
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en .
For more options, visit https://groups.google.com/groups/opt_out .
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com .
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en .
For more options, visit https://groups.google.com/groups/opt_out .
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home