How to send POST request? – Dev

The best answers to the question “How to send POST request?” in the category Dev.


I found this script online:

import httplib, urllib
params = urllib.urlencode({'number': 12524, 'type': 'issue', 'action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded",
            "Accept": "text/plain"}
conn = httplib.HTTPConnection("")
conn.request("POST", "", params, headers)
response = conn.getresponse()
print response.status, response.reason
302 Found
data =
'Redirecting to <a href=""></a>'

But I don’t understand how to use it with PHP or what everything inside the params variable is or how to use it. Can I please have a little help with trying to get this to work?


This is a solution without any external pip dependencies, but works only in Python 3+ (Python 2 won’t work):

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url="" # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()

Sample output:

  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "foo": "bar"
  "headers": {
    "Accept-Encoding": "identity", 
    "Content-Length": "7", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "", 
    "User-Agent": "Python-urllib/3.3"
  "json": null, 
  "origin": "", 
  "url": ""


If you really want to handle with HTTP using Python, I highly recommend Requests: HTTP for Humans. The POST quickstart adapted to your question is:

>>> import requests
>>> r ="", data={'number': 12524, 'type': 'issue', 'action': 'show'})
>>> print(r.status_code, r.reason)
200 OK
>>> print(r.text[:300] + '...')

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xml:lang="en" lang="en">
Issue 12524: change httplib docs POST example - Python tracker

<link rel="shortcut i...


Use requests library to GET, POST, PUT or DELETE by hitting a REST API endpoint. Pass the rest api endpoint url in url, payload(dict) in data and header/metadata in headers

import requests, json

url = ""

payload = {"number": 12524, 
           "type": "issue", 
           "action": "show"}

header = {"Content-type": "application/x-www-form-urlencoded",
          "Accept": "text/plain"} 

response_decoded_json =, data=payload, headers=header)
response_json = response_decoded_json.json()


You can’t achieve POST requests using urllib (only for GET), instead try using requests module, e.g.:

Example 1.0:

import requests


payload = {'number': 2, 'value': 1}
response =, data=payload)

print(response.text) #TEXT/HTML
print(response.status_code, response.reason) #HTTP

Example 1.2:

>>> import requests

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r ="", data=payload)
>>> print(r.text)
  "form": {
    "key2": "value2",
    "key1": "value1"

Example 1.3:

>>> import json

>>> url=""
>>> payload = {'some': 'data'}

>>> r =, data=json.dumps(payload))