data = {
        'ids': [12, 3, 4, 5, 6 , ...]

I want to send a POST request, but one of the fields should be a list of numbers. How can I do that ? (JSON?)

If your server is expecting the POST request to be json, then you would need to add a header, and also serialize the data for your request…

Python 2.x

import json
import urllib2

data = {
        'ids': [12, 3, 4, 5, 6]

req = urllib2.Request('')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))

Python 3.x

If you don’t specify the header, it will be the default application/x-www-form-urlencoded type.

I recommend using the incredible requests module.

payload = {'some': 'data'}
headers = {'content-type': 'application/json'}

response =, data=json.dumps(payload), headers=headers)

for python 3.4.2 I found the following will work:

import urllib.request
import json

body = {'ids': [12, 14, 50]}
myurl = ""

req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8')   # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)

This works perfect for Python 3.5, if the URL contains Query String / Parameter value,

Request URL =
Parameter value = 21f6bb43-98a1-419d-8f0c-8133669e40ca

import requests

data = {"name": "Value"}
r =, auth=('username', 'password'), json=data)

Here is an example of how to use urllib.request object from Python standard library.

import urllib.request
import json
from pprint import pprint

url = ""

values = {
    "first_name": "Vlad",
    "last_name": "Bezden",
    "urls": [

headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",

data = json.dumps(values).encode("utf-8")

    req = urllib.request.Request(url, data, headers)
    with urllib.request.urlopen(req) as f:
        res =
except Exception as e:

You have to add header,or you will get http 400 error.
The code works well on python2.6,centos5.4


    import urllib2,json

    postdata = {'key':'value'}

    req = urllib2.Request(url)
    data = json.dumps(postdata)

    response = urllib2.urlopen(req,data)

In the lastest requests package, you can use json parameter in method to send a json dict, and the Content-Type in header will be set to application/json. There is no need to specify header explicitly.

import requests

payload = {'key': 'value'}, json=payload)

This one works fine for me with apis

import requests

data={'Id':id ,'name': name}
r = url="https://apiurllink", data = data)

The Requests package used in many answers here is great but not necessary. You can perform a POST of JSON data succinctly with the Python 3 standard library in one step:

import json
from urllib import request

    headers={'Content-Type': 'application/json'},
        'pi': 3.14159

If you need to read the result, you can .read() from the returned file-like object and use json.loads() to decode a JSON response.