convert curl get to rest api to python - python

I have this curl command:
curl -X GET --header 'Accept: application/vnd.pagerduty+json;version=2' --header 'Authorization: Token token=y_NbAkKc66ryYTWUXYEu' 'https://api.pagerduty.com/services?time_zone=UTC&sort_by=name'
I need to convert it to python using requests library
import requests
def support(self):
services_list = requests.get('I need to convert the link to pass it here as a parameter')

import requests
def support():
headers = {
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': 'Token token=y_NbAkKc66ryYTWUXYEu'}
payloads = (
('time_zone', 'UTC'),
('sort_by', 'name'),)
services_list = requests.get('https://api.pagerduty.com/services', headers=headers, params=payloads)

Related

Convert CURL API command to Python API using requests and json

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))

Convert this curl command to Python requests

working curl command:
curl -i -XPATCH "https://api.threatstream.com/api/v1/intelligence/"
-H "Content-Type: application/json"
-H "Authorization: apikey email#email.com:password"
--data #C:\Users\ghossh6\indicators.json
requests:
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'apikey email#email.com:password',
}
data = open("C:/Users/ghossh6/indicators.json")
response = requests.patch('https://api.threatstream.com/api/v1/intelligence/', headers=headers, data=data)
Response
Currently, I only get 502 or 405 error codes. I have tried using json.loads() to load the file instead, without success.

Save m4a file after using authorization header to download

I am trying to download the m4a file of a recorded Zoom meeting from Zoom cloud. Here is the Zoom documentation on completed recording webhooks I am referencing. Specifically, I am trying to implement the section where Zoom describes the download_token in its schema explanation.
This is what I have so far:
from flask import Flask, request
import sys
import pprint
pp = pprint.PrettyPrinter(indent=2)
import requests
app = Flask(__name__)
#app.route('/notification/', methods = ['GET', 'POST'])
def notification():
if request.method == 'POST':
content = request.json
# pp.pprint(content)
if content['event'] == 'recording.completed':
process_recording(content['download_token'], content['payload']['object']['recording_files'])
return 'This should be where the webhook sends info to'
def process_recording(download_token, recordings_list):
recording = next(
(recording for recording in recordings_list if recording["recording_type"] == 'audio_only'),
None)
url = recording['download_url']
headers = {
'content-type': 'application/json',
'authorization': 'Bearer ' + download_token + ' --header content-type:'
}
response = requests.get(url, headers=headers)
if response:
print('Success!')
else:
print('An error has occurred.')
Am I converting the curl command correctly? The curl command should be in this format:
curl --request GET \
--url (download_url) \
--header 'authorization: Bearer (download_token) \
--header 'content-type: application/json'
Furthermore, how do I save the m4a file? What exactly does my response variable store?
Your header is wrong...
compare...
headers = {
'content-type': 'application/json',
'authorization': 'Bearer ' + download_token + ' --header content-type:'
}
with
--header 'authorization: Bearer (download_token) \
--header 'content-type: application/json'
The value for the authorization header should be
"Bearer (%s)" % download_token
or
f"Bearer ({download_token})"
if you use Python 3.6 or higher.

Django: convert cURL to python requests

I have cURL code that I need to convert to python code using requests library. I want to use this inside django backend software.
I tried several ways but I am getting some erros. Can someone help me with this?
Here is the cURL code:
curl -XPOST -H 'cache-control: no-cache' -H 'content-type: application/json' -H
'X-Client-Id: asdf1234' -H 'X-Client-Secret: qwer9876' -d '{
"planId":"BASIC", "planName":"Basic subscription plan", "amount":12,
"intervalType":"week", "intervals":2,"description":"This is the standard plan
for our services"}' 'https://test.cashfree.com/api/v2/subscription-plans'
In future, you can use a handy converter like this:
import requests
headers = {
'cache-control': 'no-cache',
'content-type': 'application/json',
'X-Client-Id': 'asdf1234',
'X-Client-Secret': 'qwer9876',
}
data = '{"planId":"BASIC", "planName":"Basic subscription plan", "amount":12,"intervalType":"week", "intervals":2,"description":"This is the standard planfor our services"}'
response = requests.post('https://test.cashfree.com/api/v2/subscription-plans', headers=headers, data=data)

Curl request to python request

I have a curl request as below:
curl -X POST \
https://example.com \
-H 'Content-Type: multipart/form-data' \
--form-string 'message=<messageML>Hello world!</messageML>'
How do i pass --form-string data in python request?
Thanks!
Use the files parameter to post your data, example:
import requests
url = 'https://httpbin.org/anything'
data = {'message':'<messageML>Hello world!</messageML>'}
r = requests.post(url, files=data)
print(r.text)
You don't have to use headers because 'multipart/form-data' is the default 'Content-Type' header when posting files.

Categories

Resources