简体   繁体   中英

How to execute this request using python

I wish to execute this using python. It is a RESTful request

 curl -XPOST -u 'userid:password' -H
    'Content-Type: application/json' -H 'X-Forwarded-For: 100.100.0.144' -k
    'http://myurl


    ' -d  ' jsonObject

What I have till now

import urllib2

def make_payment_request():
   manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
   manager.add_password(None, 'https://url', 'userid', 'passwrd')
   handler = urllib2.HTTPBasicAuthHandler(manager)

   director = urllib2.OpenerDirector()
   director.add_handler(handler)

   req = urllib2.Request('https://url', headers = {'Accept' : 'application/json'})

   result = director.open(req)


# To get say the content-length header
   print "this is result",result

I am getting response as None also how do I add jSonobject all with it

Use requests

import requests
import json

url = 'http://myurl'
headers = {'X-Forwarded-For': '100.100.0.144'}
user = 'user'
password = 'sekret'
data = {'foo': 'bar'}

r = requests.post(url,
                  data=json.dumps(data), auth=(user,password), headers=headers)

I am getting 'requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Just like a web browser, requests will try to validate SSL. In a browser when the SSL doesn't validate you get a warning and then you can continue on to the site if you choose.

For requests , you can achieve the same by passing verify=False to ignore SSL verification errors.

r = requests.post(url, verify=False,
                  data=json.dumps(data), auth=(user,password), headers=headers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM