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 in Python by ana1504.k
• 7,890 points
50 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 by SDeb
• 13,210 points

Related Questions In Python

0 votes
1 answer
+1 vote
4 answers

How can I concatenate str and int objects?

If you want to concatenate int or ...READ MORE

answered Oct 18, 2018 in Python by subhm
59 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
106 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
836 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 394 views
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,210 points
38 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,210 points
39 views