Python send POST with header Python send POST with header python python

Python send POST with header


Thanks a lot for your link to the requests module. It's just perfect. Below the solution to my problem.

import requestsimport jsonurl = '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 payloadheaders = {}r = requests.post(url, data=json.dumps(payload), headers=headers)print(r.content)


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 requestsimport jsonurl = 'https://somedomain.com'body = {'name': 'Maryja'}headers = {'content-type': 'application/json'}r = requests.post(url, data=json.dumps(body), headers=headers)

Source


To make POST request instead of GET request using urllib2, you need to specify empty data, for example:

import urllib2req = 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)