urllib2 and json

0 votes
I am trying to figure out how to do a POST request using urllib2 with the data being in JSON format. Can anyone help me with it?
Mar 27, 2019 in Python by ana1504.k
• 7,890 points
263 views

1 answer to this question.

0 votes
I think you need to specify a content-type header if you want it to really work. Here you include a content-type header:

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()
answered Mar 27, 2019 by SDeb
• 13,250 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How to Creat a JSON response using Django and Python?

Hello @kartik, I usually use a dictionary, not ...READ MORE

answered Aug 6 in Python by Niroj
• 79,200 points
200 views
0 votes
1 answer

Parse and restructure key:value pair of JSON Object to Python Object

@Khushboo, Can you link to a correctly formatted ...READ MORE

answered Oct 1 in Python by Gitika
• 51,880 points
154 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,577 views
0 votes
0 answers
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,250 points
147 views
0 votes
2 answers

difference between class and instance attributes

Apart from the performance, there is a ...READ MORE

answered Sep 17, 2018 in Python by SDeb
• 13,250 points
130 views