I have a curl command that works perfectly fine and gives me a HTTP 200.
curl -i -H "Authorization: Basic jadkfhjkafDSKJ12DD=" http://<ip>/LoadTest/rest/authentication-point/authenticate
The above API needs the authorization in base64 format and the details have to be passed as Headers. This is a GET request.
When I try to run the same in Python 2.7, I get Response [403]. Code below.
import requests
headers = {'Authorization': 'Basic jadkfhjkafDSKJ12DD='}
authurl = "http://<ip>/LoadTest/rest/authentication-point/authenticate"
r = requests.get(authurl, headers=headers)
print r.status_code
What am I missing here? How should i pass the authorization values exactly like I passed in the curl command? I've tried multiple ways but still end up getting HTTP 403 always. Kindly guide.
Thanks all for your inputs. This is the final solution. I found that there is proxy that is stopping the payload. So added the session to the request.
import requests
session = requests.Session()
session.trust_env = False
headers = {'Authorization': 'Basic jadkfhjkafDSKJ12DD='}
authurl = "http://<ip>/LoadTest/rest/authentication-point/authenticate"
r = session.get(authurl, headers=headers)
print r.status_code
Setting the trust_env=False ignores the following:
Authentication information from .netrc (code)
CA bundles defined in
REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE (code)
Related
I'm making a post request to some endpoint but always receiving 404 while doing it from python but when I do it with CURL everything works. Here's my python code:
import requests
def send_request(endpoint):
api_keys = {'Api-Key': API_KEY,
'Api-Username': API_USERNAME}
headers = {'content-type': 'multipart/form-data'}
request = requests.post(url = endpoint, data = api_keys, headers = headers)
print("STATUS CODE: %s" % request.status_code)
Thank you for the help!
Run
nc -lp 8080 (Linux) or nc -l 8080 (macOS)
and then make the request using curl to http://localhost:8080 and note the headers. Run the above command again and this time make the request using Python. Note the headers again and compare them with the ones you got when making the request with curl. Are they the same?
Perhaps User-Agent is missing. Make sure to add the missing headers in Python.
I managed to get 200 with following code:
def send_request(endpoint):
headers = {'Content-Type': 'multipart/form-data', 'Api-Key': API_KEY, 'Api-Username': API_USERNAME}
request = requests.post(url = endpoint, headers = headers)
print("Request Status Code: {}".format(request.status_code))
response = json.loads(request.text)
return response_text
All needed to be sent in headers
I'm using requests to pull down some JSON from a 3rd party portal.
Whenever i use requests i get a '{"Message":"Path not found"}' error
however running the command with cURL works fine. Here is the cURL command:
curl https://blah.com/api/v1/nodes --header 'Authorization: Bearer my-long-token-base64'
Trying to do this with python3 / requests i have:
#convert token to base64
base64_token = base64.b64encode(bytes(access_token, 'utf-8'))
#convert to str
base64_token = base64_token.decode("utf-8")
api = 'https://blah.com/api/v1/nodes'
headers = {'Authorization': 'Bearer ' + base64_token,
'Accept': 'application/json'}
nodes = requests.post(api, headers=headers)
whenever i run this the response is '{"Message":"Path not found"}'
I thought it might be something to do with the base64 token (which is required) but pretty sure i've got that part right because otherwise i get a 401.
any thoughts?
I'm aware seems a pretty repetitive issue. From bash and curl I make calls to the API Github this way
# curl -u myuser https://api.github.com/repos/myuser/somerepo/pulls?state=all -H "Authorization: token randomtokenhere" -d '{"title":"Pull Request develop to master","base":"master", "head":"develop"}'
and works like a charm. However, from Python 3(3.5.2) with json and requests, I got an error no matter what.
Example:
user = "myuser"
password = "sompassword"
url = "https://api.github.com/repos/myuser/somerepo/pulls?state=all"
token = "randomtokenhere"
title = "Pull Request develop to master"
base = "master"
head = "develop"
headers = {'Authorization': 'token ' + token}
content = {"title":title,"base":base, "head":head}
req = requests.post(url,json=json.dumps(content),auth=(user,password),headers=headers)
print("response posts is {} and status code is {}".format(req.text,req.status_code))
the response of requests is
response posts is {"message":"Must specify two-factor authentication OTP code.","documentation_url":"https://developer.github.com/v3/auth#working-with-two-factor-authentication"} and status code is 401
so it seems the call is just missing the token in some way. But I'm not able to know why. Can I debug this in some way? Or did I miss something very obvious?
Thanks
Well, sorry for bother, my call was not correct. This actually worked:
//req = requests.post(url,json=json.dumps(content),auth=(user,password),headers=headers)
req = requests.post(url,json=content,auth=(user,token))
I've removed the json.dumps function, removed the headers parameter, and instead use password I set up the token with the auth.
Regards
Here is the working curl statement that I am trying to put into a python script:
curl -L -H 'X-Cisco-Meraki-API-Key: <key>' -X PUT -H'Content-Type: application/json' --data-binary '{"name":"new SSID name", "enabled":true, "authMode":"psk", "encryptionMode":"wpa", "psk":"abcd1234", "ipAssignmentMode":"Bridge mode"}' 'https://dashboard.meraki.com/api/v0/networks/[networkId]/ssids/[number]'
Here is my python code.
import requests
import json
url = "https://dashboard.meraki.com/api/v0/networks/XXXXXXX/ssids/2"
headers = {'X-Cisco-Meraki-API-Key': 'YYYYYYY', 'content-type': 'application/json'}
payload = {"name":"test", "enabled":"true", "authMode":"psk", "encryptionMode":"wpa", "psk":"abcd1234", "ipAssignmentMode":"NAT mode"}
r = requests.put(url, headers=headers, data=payload, allow_redirects=True)
print r.status_code
When I run the curl command, it works, but when I try the python script, I get a 400 error message. My guess is that it has to do with the way that the application is interpreting the payload. Any help would be greatly appreciated.
You need to use:
data=json.dumps(payload)
Instead of:
data=payload
When you add 'content-type': 'application/json' header, you mean that you're sending a json data, that's why you need to use json.dumps() to sent a valide json data instead of a Python dictionary / object.
I am trying to access data using API which is behind the proxy server. If I use curl command like below it works:
curl --proxy http://MY_PROXY_SERVER:PORT --header "Accept: application/csv" http://WEB_SERVER_ADDRESS/data/CHANNEL?start=1470011400
I get the data which is expected.
When I am trying to access the same URL with python either by requests or urllib2 I am not able to get the data back. This is the code:
from __future__ import print_function
import requests
s = requests.Session()
s.proxies = {"http": "http://MY_PROXY_SERVER:PORT"}
headers = {'Accept': 'application/csv'}
url = "http://WEB_SERVER_ADDRESS/data/CHANNEL?start=1470011400"
r = s.get(url, headers=headers)
print(r.text)
I don't get any error and request is able to go through the python successfully. The output is empty list. I also tried with other media type supported by API like 'json', issue still persists.