0

I'm writing a rest service which accepts json documents with http post. I can submit my data using my "Chrome Rest Console" plugin by putting a JSON document into the raw body field.

I'm trying to achieve the same using urllib2. Using urllib2 i'm not able to post data unless I urlencode one or more key/value pairs which correspond to the cgi form fields of the web service. However, I don't want to make use of cgi form fields, as my rest service doesn't have any, I just want to submit a raw json document ...

How can I do that?

Thanks,

J

1 Answer 1

3

I'm not flagging the question as exact duplicate as I'm not sure this is what you meant. However from this other question:

import json
import urllib2
data = json.dumps([1, 2, 3])
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()
1
  • Not defining the 'Content-Type' appeared to be my problem... Obvious but it kept me looking into the wrong direction. Thanks
    – jay_t
    Dec 11, 2011 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.