3

I'm transmitting a application/json in java with a HttpPost to a server which use Django.

I am performing the transmission as described here: JSON POST request parsing in PHP

How can I create a page that receive this json data ?

Community
  • 1
  • 1
Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58

2 Answers2

7

You can receive json through request.raw_post_data

data=simplejson.loads( request.raw_post_data )
czarchaic
  • 6,268
  • 29
  • 23
  • For django 1.4+, use request.body as request.raw_post_data is deprecated. http://stackoverflow.com/a/10990800/1011746 – mindriot Jul 09 '13 at 21:09
1

the request variable in your views have a property request.POST which contains post data. this is (well technically, acts like) a dictionary. you also want to have a look at the json module in python's standard library

in the end i guess you'll want something like

def my_view(request):
    # error checking omitted here, e.g. what if nothing is posted
    # or the json is invalid etc.
    posted_json = request.POST['my_json_variable']
    my_dict = json.loads(posted_json)

    # do something with the data
second
  • 28,029
  • 7
  • 75
  • 76
  • Stop me if I'm wrong, but that would work it I submit a form where one of the field is a json variable (with `application/x-www-form-urlencoded`). Here I transmit only a json object. The java request is done as explained here : http://stackoverflow.com/questions/8391302/json-post-request-parsing-in-php/8391451#8391451 – Martin Trigaux Dec 11 '11 at 10:14
  • @MartinTrigaux: You are incorrect in so far as the request object will still contain your data. If you need more information how to access it, consider looking at the documentation, rather than complaining that second hasn't written the exact right code for you. – Marcin Dec 11 '11 at 11:49
  • Sorry but no, I've tried the code, I catch a MultiValueDictKeyError when I try to get something in the `request.POST`. And by the way, where do you see that I'm complaining ? I just ask more info because it seemed to me it wasn't working, no reason to down vote the question for that... – Martin Trigaux Dec 12 '11 at 11:22