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
Related
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))
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.
Very puzzled here - in python using requests I can use GET/POST to do a few different things: POST to request a token, and GET to get public signing key x/y coordinates. And in curl I can sign. But in python it errors.
This works in CURL:
curl -vs -d '{"alg":"ES256", "value":"***"}' -H "Authorization: Bearer REDACTED" -H "Content-Type: application/json" -X POST https://REDACTED.vault.azure.net/keys/REDACTED/REDACTED/sign?api-version=7.0
but in python this fails:
post_payload = {'alg':'ES256', 'value':'***'}
post_headers = {'Authorization': 'Bearer REDACTED', 'Content-Type': 'application/json',}
response = post('https://REDACTED.vault.azure.net/keys/REDACTED/REDACTED/sign?api-version=7.0', data=post_payload, headers=post_headers)
print(response.text)
{"error":{"code":"BadParameter","message":"Property has invalid value\r\n"}}
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.
So here is my curl command, In command line, curl script is working fine
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -u user:password URL -d '{"key1":"value1","key2":"value2"}'
Now I have converted my code to python and the code look like this :
import requests
import json
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
data = '{"key1":"value1","key2":"value2"}'
requests.post('URL', headers=headers, data=data, auth=('user', 'password'))
When I ran this code, I am not getting any output.
Please let me know what I am doing wrong in here.
At first, the 'url' parameter in requests.post should be an complete url including the 'http://' or 'https://'.
Secondly, if you need to post some data in form of application/json, using Python protogenic dict will be fine. like this
data = {"key1":"value1","key2":"value2"}
you might be looking at something like this?
r = requests.post("http://{}/Command?".format(web_addr), params=dict)
print r.text
In Python 3.6.1, you should get the response 200 message if the following code gets executed, with a existing 'URL':
from requests import post
data = {"key1":"value1","key2":"value2"}
print(post('URL', json=data))