> Hi,
> I am working on implementing a REST web service example of booking a
> hotel. It was supposed to be simple to start with but I am already
> badly stuck:)
> I am stuck with deserialization and serialization. I want to create
> a new resource when PUT is requested on booking. My GET method works
> perfectly fine but I am stuck with PUT. My code on method PUT is:
>
> def do_PUT(self):
> try:
> deserialized = serializers.deserialize("json",
> self.request.raw_post_data)
> put_book = list(deserialized)
> except (ValueError, TypeError, IndexError):
> response = HttpResponse()
> response.status_code = 400
> return response
> for obj in put_book:
> b1 = guest( fName = "hello", phone = 21, email =
> "test@email")
> b1.save()
>
> json = serializers.serialize("json", b1)
> response = HttpResponse(json, mimetype="application/json")
> response.status_code = 201
> response["Location"] = "/bookings/%s" % (b1.id)
> return response
>
> Now when I send PUT request via curl, i.e.:
>
> curl -i -d "{'room_id': 21, 'payment_id': 21, 'guest_id': 21}" -X PUThttp://127.0.0.1:8000/bookings/
>
> It gives me 400 error, i.e. HTTP/1.0 400 BAD REQUEST
> This means it is raising and exception and not able to deserialize the
> data.
>
> I tried removing the statement: put_book = list(deserialized) in
> try block. ie,
>
> try:
> deserialized = serializers.deserialize("json",
> self.request.raw_post_data)
> # put_book = list(deserialized)
> except (ValueError, TypeError, IndexError):
> response = HttpResponse()
> response.status_code = 400
> return response
> for obj in deserialized:
> b1 = guest( fName = "hello", phone = 21, email =
> "test@email")
> b1.save()
>
> json = serializers.serialize("json", b1)
> response = HttpResponse(json, mimetype="application/json")
> response.status_code = 201
> response["Location"] = "/bookings/%s" % (b1.id)
> return response
>
> It then doesnt raises and exception but gives 500 error: HTTP/1.0 500
> INTERNAL SERVER ERROR
> .....
> ....
> Exception Type: ValueError at /bookings/
> Exception Value: Expecting property name: line 1 column 1 (char 1)
> ....
> ...
> In line 1 of my views.py, I am just importing : from django.http
> import HttpResponse, HttpResponseNotAllowed, HttpResponseForbidden
> And there is no problem in this line, since my rest of the views
> perfectly fine if its not for this problem.
>
> Any help and input would be appreciated. I am stuck in this for last 2
> days and trying to fix it. I have googled extensively but still no
> clue.
>
> Thanks,
> Irum
The line1, column 1 refers to the JSON file, not the code. Your JSON
is not valid. Try putting it through a validator like http://jsonviewer.stack.hu/
to see what's wrong.
That said, if you want to create a RESTful API in Django, the work has
already been done for you: just use django-piston.
--
DR.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment