How to turn python dict into Curl - python

For some reasons my requests to an API is failing. This is the code:
def create_alert(alert_dict)
url = host + '/api/alerts'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer tokengen'
}
response = requests.request('POST', url, headers = headers, data=json.dumps(alert_dict))
return response
alert_dict = {
"priority": "1",
"to": to,
"from": from_
}
response = create_alert(alert_dict)
print(response)
The way I'm doing it is saving everything to a dictionary, then making the POST request with it. But for some reason im getting a 500 error.
This is the curl that is working
curl --location --request POST "http://newhost.com/api/alerts" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer tokengen" \
--data "{
\"to\": \"sample#email.com\",
\"from\": \"from#email.com\",
\"priority\": \"1\",
}"
How should I convert my dict in a way that it would work just like the curl --data when requested

Related

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.

Why does the github API python POST request not work?

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?

How should this PUT curl be converted to python requests?

How can I convert this CURL PUT request to python requests:
The curl is
curl -X PUT "https://example.com" -H "accept: application/json" -H "Content-Type: application/json-patch+json" -d "{ \"userName\": \"exampleuser\", \"password\": \"examplepass\"}"
Currently got
headers = {"accept": "application/json",
"Content-Type": "application/json-patch+json"}
data = {'\"userName\"': '\"exampleuser\"',
'\"password\"': '\"examplepass\"'}
response = requests.put(url=url, data=data, headers=headers)
print(response)
Currently getting a 401 response. Unfortunately, the curl converter does not recognise it.
In bash, you escaped the quotes of the json
In Python, you shouldn't need to
data = {'userName' : 'exampleuser',
'password': 'examplepass'}
Then, you're sending json, so do json=data instead of data=data

Python requests library is not working, while cURL is working

I need to retrieve a JWT (JSON Web Token) from a Microsoft API using Python (check this API documentation for Microsoft Graph)
The following Python code using the requests library does not work giving HTTP response code 400, however, the equivalent cURL command does work giving back the expected JSON containing the JWT.
Python / requests code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com/{}/oauth2/v2.0/token".format(tenant)
http_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
http_query_params = {
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
}
http_response = requests.post(token_url, params=http_query_params, headers=http_headers)
cURL command:
curl -v -X POST \
--data-urlencode 'client_id=<MY_FOO_C_ID>' \
--data-urlencode 'scope=<MY_FOO_SCOPE>' \
--data-urlencode 'client_secret=<MY_FOO_C_SECRET>' \
--data-urlencode 'grant_type=client_credentials' \
-H 'Content-Type: application/x-www-form-urlencoded' \
'https://login.microsoftonline.com/<MY_FOO_TENANT>/oauth2/v2.0/token'
From the verbose output of the requests library I can see that it is URL encoding all those HTTP query parameters, so I tend to think that should not be the problem.
what's wrong with the Python implementation?
how to make it work?
you should pass http_query_params as data instead of params. try the following code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com/{}/oauth2/v2.0/token".format(tenant)
http_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
http_body = {
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
}
http_response = requests.post(token_url, data=http_body, headers=http_headers)
hope this helps

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.

Categories

Resources