I have a curl command as follows that can make a restful API call to my webservice:
curl 'https://my-web-service/...' -H 'Connection: keep-alive' -H 'Cookie: csrftoken=xxx; sessionid=xxx'
Now how do I have such command ported in python3 code?
Here is what I have initial coded in python3, which is not working yet.
import requests
csrftoken = 'xxx'
sessionid = 'xxx'
api_url = "https://my-web-service/..."
header = {"Cookie": ? }
response = requests.get(api_url, headers=header)
print(response.status_code)
Because I don't know how to fit both sessionid and csrftoken value in the hearder part. Any ideas ?
Thanks,
Jack
headers can typically be passed as a dictionary via requests.
import requests
url = "https://my-web-service/..."
headers = {'Cookie': 'xxx', 'sessionid': 'xxx'}
response = requests.get(url, headers=headers)
Related
I've have been accessing an supportpal API via curl just fine using the following command. (https://docs.supportpal.com/current/REST+API)
curl.exe -i -u 'APIKEY:x' -X GET https://support.url.org/api/user/user/3697
This correctly grabs the data. I've trying replicate this with python but i continually have issues with authentication and get the following error.
Failed to authenticate because of bad credentials or an invalid authorization header
The code i'm using is straight forward.
import requests
import json
url = "https://support.url.org/api/user/user/3697"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer: {APIKEY:x}"
}
response = requests.request("GET", url, headers=headers)
print(response. Text)
I'm thinking i have an issue with the auth header, but can't figure it out.
from requests.auth import HTTPBasicAuth
import requests
url = 'https://support.url.org/api/user/user/3697'
headers = {'Accept': 'application/json'}
auth = HTTPBasicAuth('apikey', x)
req = requests.get(url, headers=headers, auth=auth)
I am connecting through an API to receive data. From the website API documentation, the instructions use either two CURL methods to connect the API; however, I need to connect using python.
1st Method
Curl Example
curl -d '' -X POST 'https://api.bcda.cms.gov/auth/token' \
-H "accept: application/json" \
-H "authorization: Basic <Client_Secret>"
My Python Conversion:
import requests
import json
url = 'https://api.bcda.cms.gov/auth/token'
headers = {"accept": "application/json", "authorization": 'Basic',
'<API_Key>': '<API_Secret>'}
r = requests.post(url = url, data ={}, headers = headers)
print(r)
2nd Method Curl
curl -d '' -X POST 'https://api.bcda.cms.gov/auth/token' \
--user <Client_Key>:<Client_Secret> \
-H "accept: application/json"
My 2nd Python conversion code:
import requests
import json
url = 'https://api.bcda.cms.gov/auth/token'
user = {"<Client_Key>":"<Client_Secret>", "accept": "application/json"}
r = requests.post(url = url, headers = user)
print(r)
I am receiving a 403 connection error, meaning "response status code indicates that the server understands the request but refuses to authorize it."
You should use auth parameter and not headers to convert --user option
headers = {'accept': 'application/json'}
r = requests.post(url=url, headers=headers, auth=(client_key, client_secret))
I am trying to access the github API via requests with python (Answers in similar questions here and here do not help).
Using curl, I am able to get a list of recent commits for a project, e.g.
curl -H "Accept: application/vnd.github.inertia-preview+json Authorization: token a0d42faabef23ab5b5462394373fc133ca107890" https://api.github.com/repos/rsapkf/42/commit
Trying to use the same setup in python with requests I tried to use
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json", "Authorization": "token a0d42faabef23ab5b5462394373fc133ca107890"}
r = requests.get(url, headers=headers)
as well as
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
my_username = "someuser"
my_token = "a0d42faabef23ab5b5462394373fc133ca107890"
r = requests.get(url, headers=headers, auth=(my_username, my_token))
but in both cases I get the response
{'documentation_url': 'https://docs.github.com/rest',
'message': 'Bad credentials'}
What am I missing here?
No Authentication is required. The following works:
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
r = requests.get(url, headers=headers)
When using the shell, I can successfully create a new user by running
curl --user administrator:pasword "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d #user.json
However when I try to do the same thing in python using requests, I get a 200 response and no user is created.
This is the script I am using:
import requests
headers = {
'Content-Type': 'application/json',
}
data = open('user.json')
response = requests.post('https://localhost:8080/midpoint/ws/rest/users', headers=headers, data=data, auth=('Administrator', 'password'))
print(response)
To me they look the same. What is different in the python request that is stopping the user from being created?
I compare the post date with curl and python requests.And i found the difference.
CURL: {"user" : "hero","pd":30}
Requests: pd=30&user=hero
Then this is my test.
import requests
import json
headers = {
'Content-Type': 'application/json',
}
with open('user.json') as j:
data = json.load(j)
response = requests.post('http://127.0.0.1:8080',
headers=headers,
json = data,
auth=('Administrator', 'password'))
print(response.headers)
I think using json = data would work as well, but I was finally successful using json dumps: data=json.dumps(data)
I am trying to make API requests with proper authorization. I looked at Conversion of curl to python Requests but am having trouble putting the authorization headers.
This is what I have so far:
import base64
import http.client
import requests
import json
headers = {
"Content-Type": "application/json",
'Authorization': 'Basic %s' % base64.b64encode(b'username:password').decode('ascii'),
}
payload = {json.dumps({"one":["two:three:four"]})}
url = 'https://website/v1'
r = requests.post(url, data=payload, headers=headers)
if __name__=='__main__':
print (r.text)
The only difference between the link provided above and my code is that he has query={"tags":["test1","test2"]} I do not have that at all.
This is the curl I am trying to translate to get what I got above.
curl -X POST -u "username:password" -H "Content-Type:application/json" "https://website.com" -d '{"ids":["something:numberOne:numbertwo"]}'
Any help appreciated.