Clone curl with python-requests - python

This curl request works:
curl -H 'content-type: application/json' --inient_id":"?CLIENT_ID??", "client_secret":"CLIENT_SECRET", "grant_type":"client_credentials", "scope": "anonymous"}' https://auth2do2go.fitdev.ru/oauth/token
This python request does not:
data = {
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET",
"grant_type": "client_credentials", "scope": "anonymous"
}
url = "https://auth2do2go.fitdev.ru/oauth/token/"
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers, data=data, verify=False)
print(r.content)
Why it this happening?
I've tried data=json.dumps(data) instead of data=data, still no luck.

Your data isn't JSON:
import json
r = requests.get(url, headers=headers, data=json.dumps(data), verify=False)
Also, don't post live credentials here ;)

Related

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)

convert curl command to a Python request command

I am trying to convert the following curl command to Python request command:
curl 'https://api.test.com/v1/data/?type=name&status=active' -H 'Authorization: apikey username:api_key_value'
I have tried a lot of possible solutions like the one below but nothing is working and I am getting a'401' code:
url = "https://api.test.com/v1/data/?type=name&status=active"
headers = {"content-type": "application/json", "Accept-Charset": "UTF-8"}
params = (
(username, api_key_value),
)
data = requests.get(url, headers=headers, params=params).json
print(data)
Params specifies the URL parameters. The Authorization thing is just another header. And response.json is a function.
url = "https://api.test.com/v1/data/"
headers = {
"content-type": "application/json",
"Accept-Charset": "UTF-8",
"Authorization": "apikey {0}:{1}".format(username, api_key_value)
}
params = { 'type': 'name', 'status': 'active' }
data = requests.get(url, headers=headers, params=params).json()
print(data)

Convert CURL request to python 3

I have this curl request I would like to convert to python 3
curl -X "POST" "https://conversations.messagebird.com/v1/send" \\
-H "Authorization: AccessKey YOUR-API-KEY" \\
-H "Content-Type: application/json" \\
--data '{ "to":"+31XXXXXXXXX", "from":"WHATSAPP-CHANNEL-ID", "type":"text", "content":{ "text":"Hello!" }, "reportUrl":"https://example.com/reports" }'
can anyone help me please?
I've tried the following request, but not working :
import requests
header = {"Authorization":"AccessKey YOUR-API-KEY"}
data = { "to":"+31XXXXXXXXX", "from":"WHATSAPP-CHANNEL-ID", "type":"text", "content":{"text":"Hello!" }, "reportUrl":"https://example.com/reports"}
url = 'https://conversations.messagebird.com/v1/send'
response = requests.post(url, data=data, headers=header)
print(response.text)
I'm having the error message :
<Response [400]>
{"errors":[{"code":21,"description":"JSON is not a valid format"}]}
you can use json instead of data,
requests.post('http://httpbin.org/post', json={"key": "value"})
you need dump data
requests.post(url, data=json.dumps(data), headers=headers)
thers is another question like yours.
According to their documentation, the cURL is correct and would be implemented as is in Python:
import requests
header = {
"Authorization": "AccessKey YOUR-API-KEY",
"Content-Type": "application/json"
}
data = {
"to": "+31XXXXXXXXX",
"from": "WHATSAPP-CHANNEL-ID",
"type": "text",
"content": {"text":"Hello!"},
"reportUrl": "https://example.com/reports"
}
url = 'https://conversations.messagebird.com/v1/send'
response = requests.post(url, json=data, headers=header)
print(response.json())

Python: conversion from requests library to urllib3

I need to convert the following CURL command into an http request in Python:
curl -X POST https://some/url
-H 'api-key: {api_key}'
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{ "data": { "dirname": "{dirname}", "basename": "{filename}", "contentType": "application/octet-stream" } }'
I initially successfully implemented the request using Python's requests library.
import requests
url = 'https://some/url'
api_key = ...
dirname = ...
filename = ...
headers = {
'api-key': f'{api_key}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
payload = json.dumps({
'data': {
'dirname': f'{dirname}',
'basename': f'{filename}',
'contentType': 'application/octet-stream'
}
})
response = requests.post(url, headers=headers, data=payload)
The customer later asked not to use pip to install the requests library. For this I am trying to use the urllib3 library as follows:
import urllib3
url = 'https://some/url'
api_key = ...
dirname = ...
filename = ...
headers = {
'api-key': f'{api_key}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
payload = json.dumps({
'data': {
'dirname': f'{dirname}',
'basename': f'{filename}',
'contentType': 'application/octet-stream'
}
})
http = urllib3.PoolManager()
response = http.request('POST', url, headers=headers, body=payload)
The problem is that now the request returns me an error 400 and I don't understand why.
Try calling .encode('utf-8') on payload before passing as a parameter.
Alternatively, try to pass payload as fields without manually converting it to JSON:
payload = {
'data': {
'dirname': f'{dirname}',
'basename': f'{filename}',
'contentType': 'application/octet-stream'
}
}
http = urllib3.PoolManager()
response = http.request('POST', url, headers=headers, fields=payload)

Get Oauth Access Token in Python

I am trying to generate a oauth access token for experian's sandbox API (they give information on credit information).
Their tutorial Says to run this (fake data) to get an access token:
curl -X POST
-d '{ "username":"youremail#email.com", "password":"YOURPASSWORD"}'
-H "Client_id: 3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9"
-H "Client_secret: ipu3WQDqTEjqZDXW"
-H "Content-Type: application/json"
"https://sandbox-us-api.experian.com/oauth2/v1/token"
How would I run this in python? I tried this among a lot of other things:
data = { "username" : "youremail#email.com", "password":"YOURPASSWORD"}
headers = {"Client_id": "3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9", "Client_secret": "ipu3WQDqTEjqZDXW", "Content-Type": "application/json"}
response = requests.post("https://sandbox-us-
api.experian.com/oauth2/v1/token", data=data, headers=headers)
Any help would be greatly appreciated
Almost there, just need to parse the response:
import json
data = { "username" : "youremail#email.com", "password":"YOURPASSWORD"}
headers = {"Client_id": "3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9", "Client_secret": "ipu3WQDqTEjqZDXW", "Content-Type": "application/json"}
response = requests.post("https://sandbox-us-api.experian.com/oauth2/v1/token", data=data, headers=headers)
if response.status_code in [200]:
tok_dict = json.loads(response.text)
print(tok_dict)
issued_at = tok_dict["issued_at"]
expires_in = tok_dict["expires_in"]
token_type = tok_dict["token_type"]
access_token = tok_dict["access_token"]
else:
print(response.text)

Categories

Resources