Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding x-www-form-urlencoded to post request

Tags:

python

example from postman

import urllib2
url = "http://www.example.com/posts"
    
req = urllib2.Request(url,headers={'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30" , "Content-Type": "application/x-www-form-urlencoded"})
con = urllib2.urlopen(req)
print con.read()

now this code works fine but i want to add the value as you see in postman picture to get the response that i want i don't know how to add the key and value postid = 134686 to python and it's post request in postman

like image 317
Amine Avatar asked Mar 11 '17 14:03

Amine


1 Answers

Form-encoded is the normal way to send a POST request with data. Just supply a data dict; you don't even need to specify the content-type.

data = {'postid': 134786}
headers = {'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30"}
req = urllib2.Request(url, headers=headers, data=data)
like image 53
Daniel Roseman Avatar answered Sep 26 '22 22:09

Daniel Roseman