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

jay_t
  • 3,593
  • 4
  • 22
  • 27

1 Answers1

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()
Community
  • 1
  • 1
mac
  • 42,153
  • 26
  • 121
  • 131
  • 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 '11 at 22:22