Baruch
Baruch

Reputation: 21528

POST parameters formatted as JSON

I am writing a web service using Django that will be consumed from a MS SharePoint workflow. In the SP workflow, I created a dictionary with 2 items (id:1, text:'foo'), and used this dictionary as the request content. However, instead of using the dictionary to format a traditional POST parameter list, it sends it as a JSON object in the body of the POST request, so instead of the expected:

id=1&text=foo

in the body of the request, there is this:

{"id":1,"text":"foo"}

which of course, in turn, does not get parsed correctly by Python/Django (I am not sure who exactly does the parsing). How can I either get it to parse JSON, or get SharePoint to send traditionally encoded POST parameters?

EDIT
I saw other posts that explain how to get the raw body and parse the JSON. I was looking for a solution that would either:

  1. Make SharePoint send normal data, or
  2. Get Django to respect the Content-type header that states the data is JSON

Upvotes: 0

Views: 118

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599866

There is no need for any parsing at the framework level. The body of the post request is always available in request.body, so you can access it directly:

result = json.loads(request.body)

Upvotes: 1

Vipul Bhatt
Vipul Bhatt

Reputation: 382

May be it will help you bit more to handle.

import json
import urlparse

json.dumps(urlparse.parse_qs("id=1&text=foo"))

Upvotes: 0

Related Questions