Python send POST with header
263,397
Solution 1
Thanks a lot for your link to the requests module. It's just perfect. Below the solution to my problem.
import requests
import json
url = 'https://www.mywbsite.fr/Services/GetFromDataBaseVersionned'
payload = {
"Host": "www.mywbsite.fr",
"Connection": "keep-alive",
"Content-Length": 129,
"Origin": "https://www.mywbsite.fr",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5",
"Content-Type": "application/json",
"Accept": "*/*",
"Referer": "https://www.mywbsite.fr/data/mult.aspx",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Cookie": "ASP.NET_SessionId=j1r1b2a2v2w245; GSFV=FirstVisit=; GSRef=https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CHgQFjAA&url=https://www.mywbsite.fr/&ei=FZq_T4abNcak0QWZ0vnWCg&usg=AFQjCNHq90dwj5RiEfr1Pw; HelpRotatorCookie=HelpLayerWasSeen=0; NSC_GSPOUGS!TTM=ffffffff09f4f58455e445a4a423660; GS=Site=frfr; __utma=1.219229010.1337956889.1337956889.1337958824.2; __utmb=1.1.10.1337958824; __utmc=1; __utmz=1.1337956889.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)"
}
# Adding empty header as parameters are being sent in payload
headers = {}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print(r.content)
Solution 2
If we want to add custom HTTP headers to a POST request, we must pass them through a dictionary to the headers
parameter.
Here is an example with a non-empty body and headers:
import requests
import json
url = 'https://somedomain.com'
body = {'name': 'Maryja'}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(body), headers=headers)
Solution 3
To make POST request instead of GET request using urllib2
, you need to specify empty data, for example:
import urllib2
req = urllib2.Request("http://am.domain.com:8080/openam/json/realms/root/authenticate?authIndexType=Module&authIndexValue=LDAP")
req.add_header('X-OpenAM-Username', 'demo')
req.add_data('')
r = urllib2.urlopen(req)
Related videos on Youtube
Author by
Matt
Updated on December 07, 2020Comments
-
Matt over 3 years
I try to build a python script who sends a POST with parameters for extracting the result. With fiddler, I have extracted the post request who return that I want. The website uses https only.
POST /Services/GetFromDataBaseVersionned HTTP/1.1 Host: www.mywbsite.fr "Connection": "keep-alive", "Content-Length": 129, "Origin": "https://www.mywbsite.fr", "X-Requested-With": "XMLHttpRequest", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5", "Content-Type": "application/json", "Accept": "*/*", "Referer": "https://www.mywbsite.fr/data/mult.aspx", "Accept-Encoding": "gzip,deflate,sdch", "Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4", "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3", "Cookie": "ASP.NET_SessionId=j1r1b2a2v2w245; GSFV=FirstVisit=; GSRef=https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CHgQFjAA&url=https://www.mywbsite.fr/&ei=FZq_T4abNcak0QWZ0vnWCg&usg=AFQjCNHq90dwj5RiEfr1Pw; HelpRotatorCookie=HelpLayerWasSeen=0; NSC_GSPOUGS!TTM=ffffffff09f4f58455e445a4a423660; GS=Site=frfr; __utma=1.219229010.1337956889.1337956889.1337958824.2; __utmb=1.1.10.1337958824; __utmc=1; __utmz=1.1337956889.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)" {"isLeftColumn":false,"lID":-1,"userIpCountryCode":"FR","version":null,"languageCode":"fr","siteCode":"frfr","Quotation":"eu"}
And now my python script:
#!/usr/bin/env python # -*- coding: iso-8859-1 -*- import string import httplib import urllib2 host = "www.mywbsite.fr/sport/multiplex.aspx" params='"isLeftColumn":"false","liveID":"-1","userIpCountryCode":"FR","version":"null","languageCode":"fr","siteCode":"frfr","Quotation":"eu"' headers = { Host: www.mywbsite.fr, "Connection": "keep-alive", "Content-Length": 129, "Origin": "https://www.mywbsite.fr", "X-Requested-With": "XMLHttpRequest", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5", "Content-Type": "application/json", "Accept": "*/*", "Referer": "https://www.mywbsite.fr/data/mult.aspx", "Accept-Encoding": "gzip,deflate,sdch", "Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4", "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3", "Cookie": "ASP.NET_SessionId=j1r1b2a2v2w245; GSFV=FirstVisit=; GSRef=https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CHgQFjAA&url=https://www.mywbsite.fr/&ei=FZq_T4abNcak0QWZ0vnWCg&usg=AFQjCNHq90dwj5RiEfr1Pw; HelpRotatorCookie=HelpLayerWasSeen=0; NSC_GSPOUGS!TTM=ffffffff09f4f58455e445a4a423660; GS=Site=frfr; __utma=1.219229010.1337956889.1337956889.1337958824.2; __utmb=1.1.10.1337958824; __utmc=1; __utmz=1.1337956889.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)" } url = "/Services/GetFromDataBaseVersionned" # POST the request conn = httplib.HTTPConnection(host,port=443) conn.request("POST",url,params,headers) response = conn.getresponse() data = response.read() print data
But when I run my script, I have this error:
socket.gaierror: [Errno -2] Name or service not known
-
ThiefMaster almost 12 yearsThere is no good reason to speak HTTP manually. Use urllib or even better, requests.
-
Nitanshu almost 6 yearsRefer this answer for better explanation. stackoverflow.com/a/51124572/7384392
-
-
Thiago almost 8 yearsWhy are you sending the header as the data of the request and why that works?
-
Antoine Bolvy over 7 yearsFYI you should NOT manually send headers like this. It's request's job. The maximum you should have to send is the user agent and custom headers.
-
py_ios_dev about 6 yearswhat does json.dumps do?
-
Rohit-Pandey over 5 yearscan we send headers using
post
inrequests
python
? -
simhumileco about 5 yearsYes, of course, @Rohit-Pandey. You can look above or here: stackoverflow.com/a/54493776/4217744
-
Engin Yapici almost 4 yearsYou don't need
json.dumps
-
Willy satrio nugroho over 3 years@Engin Yapici Usually we Don't need to dump our data as json but in some special case we need it. Such as sending post request in FASTAPI, It's requires to dump our data as json.
-
elPastor over 3 years@Willysatrionugroho - agreed. I was trying to pass a payload without
json.dumps()
and it failed, got very frustrated. Found this answer, usedjson.dumps()
and the API works like a charm. -
slayer over 3 years@AntoineBolvy Interesting information. Can you provide more detail or a specific resource to read about that? I can't find any. Thankful in advance.
-
slayer over 3 years@py_ios_dev It converts Python objects into JSON format data.
-
Ahmed Yossef about 2 yearsThis reply is correct, worked for me using body to post specific keys that are supported by the server