How to Run this API Request Using Python - python

I am very new to python and API Integration. Can anyone pls tell me how do I run this okta document using python?
curl -v -X GET
-H "Accept: application/json"
-H "Content-Type: application/json"
-H "Authorization: SSWS ${api_token}"
"https://${yourOktaDomain}/api/v1/apps"

You can use urllib for do it.
Example:
from urllib.request import Request, urlopen
api_token = "..."
yourOktaDomain = "..."
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Autorization": "SSWS " + api_token
}
request = Request("https://" + yourOktaDomain + "/api/v1/apps", headers=headers)
response = urlopen(request)
data = response.read()

import requests
import json
yourOktaDomain = 'something.okta.com'
api_token= ''
payload = ""
headers = {
'Accept': "application/json",
'Content-Type': "application/json",
'Authorization': "SSWS "+api_token
}
response1=requests.request("GET", 'https://'+{yourOktaDomain}+'/api/v1/apps', data=payload, headers=headers)
response1.json()

Related

Curl command works fine, while Python requests returns 404

Curl
curl "https://api.wanikani.com/v2/summary" \ -H "Wanikani-Revision: 20170710" \ -H "Authorization: Bearer <API-KEY>"
This command returns the expected json.
Python code
import requests
headers = {"Wanikani-Revision": "20170710", "Authorization": "Bearer <API-KEY>"}
res = requests.post('https://api.wanikani.com/v2/summary', headers = headers)
print(res.text)
This code returs 404.
{"error":"Not found","code":404}
That function only accepts GET. Look https://docs.api.wanikani.com/20170710/#summary
Try:
import requests
headers = {"Wanikani-Revision": "20170710", "Authorization": "Bearer <API-KEY>"}
res = requests.get('https://api.wanikani.com/v2/summary', headers = headers)
print(res.text)

Curl To Python Request (-d parameter)

Havin error to convert the below curl into python request.
curl -G "https://thethings.example.com/api/v3/as/applications//packages/storage/uplink_message"
-H "Authorization: Bearer $API_KEY"
-H "Accept: text/event-stream"
-d "limit=10"
-d "after=2020-08-20T00:00:00Z"
-d "field_mask=up.uplink_message.decoded_payload"
Python Request
headers = {
'Authorization': f'Bearer {apiKey}',
'Accept': 'text/event-stream',
'Content-Type': 'application/json;charset=utf-8'}
data = {
"order": {
'field_mask=up.uplink_message.decoded_payload'
}
}
response = requests.get(url, headers=headers)
print(response.status_code)
print(response.text)
you can try use this tool - https://curlconverter.com/
import os
import requests
API_KEY = os.getenv('API_KEY')
headers = {
'Authorization': f"Bearer {API_KEY}",
'Accept': 'text/event-stream',
}
params = (
('limit', '10'),
('after', '2020-08-20T00:00:00Z'),
('field_mask', 'up.uplink_message.decoded_payload'),
)
response = requests.get('https://thethings.example.com/api/v3/as/applications//packages/storage/uplink_message', headers=headers, params=params)

422 when posting file with requests

So I've been trying to figure this out all day but haven't been able to make any progress. I have this curl command, which works:
curl -X POST -H "x-hermes-key: <KEY>" -H "Accept: application/json" --form file='#example_files/ex1.pdf' <URL> -kv
When I try to run its equivalent in python with requests, I get a 422 error:
header = {
"Accept": "application/json",
"X-Hermes-Key": <KEY>
}
f = {'file': open("example_files/ex1.pdf", "rb")}
r_create = requests.post(url=<URL>, headers=header, files=f)
Can anyone help me see where I'm making a mistake?
The following should match the curl command and POST with content-type=multipart/form-data.
You can set the filename, content_type, and headers explicitly in a tuple for the 'file' value.
headers = {
"Accept": "application/json",
"X-Hermes-Key": <KEY>
}
url = <URL>
files = [('file', ('ex1.pdf', open('example_files/ex1.pdf', 'rb'),
'application/octet-stream'))]
r = requests.post(url=url, headers=headers, files=files)
print(r.status_code)

Convert from curl python Request Error error 401

Im having trouble converting the curl request into a python code request.
Working Curl request
curl -X POST "http://xxxxxx" -H "accept: application/json" -H
"Content-Type: application/x-www-form-urlencoded" -H "Authorization:
Token 882a6ec053ff6dbac623eff400f67c0bb6ade399" -d "name=namename"
Not working python request
headers = {
'Authorization ': 'Token ' + "token",
'Content-Type': 'application/json',
}
data= {'name': "name"}
r = requests.post(
host_scheme + "://" + host_netloc + "/xxxxx",
data=json.dumps(data),
headers=headers
)
The response of the error is it cannot read the token {"detail": "Authentication credentials were not provided."} when using the python code above.
Any suggestions?
requests.post("http://xxxxxx",
data='name=namename',
headers={
"Authorization": "Token 882a6ec053ff6dbac623eff400f67c0bb6ade399",
"Content-Type": "application/x-www-form-urlencoded",
"accept": "application/json"
},
cookies={},
)
I used Uncurl. I had to remove the -X POST.

How to verify outgoing HTTP request from python's module?

I need to write Python equivalent code for below mentioned working curl(I have replaced the credentials for obvious reason, but it gives back 200 status.).
curl -X POST \
'https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123' \
-H 'Authorization: Basic token_123' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: multipart/form-data' \
-H 'Postman-Token: 58cafa90-7ae4-47db-a144-4e9d430ffc94' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'files[]=#/Users/gaurav/lever_resume.pdf' \
-F 'emails[]=a#b.com'
So, I ended up in writing this snippet.
user_email = 'user#domain.com'
admin_id = '20f3975a-543f-4ca8-b215-2f851232a0ad'
client_id = '893728937298'
client_secret = '32032'
file_path = '/Users/ttn/Desktop/a.txt'
file_name = 'a.txt'
logging.basicConfig(level=logging.DEBUG)
url = "https://api.lever.co/v1/candidates"
files = {
'files[]': (file_name, open(file_path,'rb')),
}
auth = HTTPBasicAuth(client_id, client_secret)
querystring = {
"perform_as": admin_id,
"dedupe": 'true'
}
payload = {
'emails[]': user_email
}
headers = {
'Content-Type': "multipart/form-data",
"Cache-Control": "no-cache",
"content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
}
response = requests.post(url,
headers=headers,
params=querystring,
data=payload,
auth=auth,
files=files)
req = response.request
# print(curlify.to_curl(req))
print('\n==== Headers', req.headers)
print('\n==== Body', req.body)
print('\n==== form-data', str(req))
print(response.text)
Question
Since Python version of Curl is not working(giving 502 error instead of 200), so How can I compare the two? Can I generate the Curl out of Python's request`?
Can someone spot mistake in my Python version? I am suspecting some problem at form-data being passed (to collect evidence, I need answer to above question)
Edit
There seems to be a curlify package. But It looks like it does not support distinction between -d and -F parameters.
Try this:
import requests
headers = {
'Authorization': 'Basic token_123',
'Cache-Control': 'no-cache',
'Content-Type': 'multipart/form-data',
'Postman-Token': '58cafa90-7ae4-47db-a144-4e9d430ffc94',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
}
params = (
('dedupe', 'true'),
('perform_as', 'user_123'),
)
files = {
'files[]': ('/Users/gaurav/lever_resume.pdf', open('/Users/gaurav/lever_resume.pdf', 'rb')),
'emails[]': (None, 'a#b.com'),
}
response = requests.post('https://api.lever.co/v1/candidates', headers=headers, params=params, files=files)
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response = requests.post('https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123', headers=headers, files=files)oduced version is not "correct".
# response = requests.post('https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123', headers=headers, files=files)
Reference: https://curl.trillworks.com/#python

Categories

Resources