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?
Related
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
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
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
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.
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=535fb089-9ff3-47b6-9bfb-4f1264799865&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=qWgdYAmab0YSkuL1qKv5bPX&grant_type=client_credentials' 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
Here is the command.
How about this sample script?
Sample script :
import json
import requests
url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
payload = {
"client_id": "535fb089-9ff3-47b6-9bfb-4f1264799865",
"client_secret": "qWgdYAmab0YSkuL1qKv5bPX",
"grant_type": "client_credentials",
"scope": "https://graph.microsoft.com/.default"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
res = requests.post(url, headers=headers, data=json.dumps(payload))
print(res.text)
If I misunderstand your question, I'm sorry.
You could use os (though it's outdated)
import os
os.system("curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=535fb089-9ff3-47b6-9bfb-4f1264799865&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=qWgdYAmab0YSkuL1qKv5bPX&grant_type=client_credentials' 'https://login.microsoftonline.com/common/oauth2/v2.0/token'")