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())
Related
The API instructions that I'm following state I should do the following:
# Log in with a valid username and password to receive an authorization code.
curl -X POST "https://api.sensorpush.com/api/v1/oauth/authorize" -H "accept: application/json" -H "Content-Type: application/json" -d #- <<BODY
{ "email": "me#email.com", "password": "abc123" }
BODY
I'm not very familiar with curl requests. After doing some research, I rewrote this in Python as:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "applications/json"
data = "#- <<BODY { 'email': 'me#email.com', 'password': 'abc123' } BODY"
resp = requests.post(url, headers=headers, data=data)
print(resp.status_code)
I expected to receive an authorization code, but all it returns is "412" (with the correct email and password inserted). This error code tells me access was denied, so I'm wondering what was incorrect about my Python code?
Can you try this and see if it works?
import requests
import json
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = {'Content-Type':'application/json', 'accept':'application/json'}
data = { "email": "me#email.com", "password": "abc123" }
data = json.dumps(data)
r = requests.post(url, data=data, headers=headers)
print(r.json())
I want to create a pull request on github using the API. The following curl command does work
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" -H "Authorization: token ghp_abc...123" \
https://api.github.com/repos/alex4200/hello-world/pulls \
-d '{"head":"copyright_updater","base":"master","title":"Test PR curl"}'
but this python snippet does not work:
import requests
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token ghp_abc...123"
}
data = {
"head": "copyright_updater",
"base": "master",
"title": "Test PR python"
}
response = requests.post(
"https://api.github.com/repos/alex4200/hello-world/pulls",
headers=headers,
data=data
)
print(response.text)
This command, with the exact same data as the curl, returns a 400 error with the message
{"message":"Problems parsing JSON","documentation_url":"https://docs.github.com/rest/reference/pulls#create-a-pull-request"}
What is the problem here?
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)
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)
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 ;)