I need to convert a curl statement to Python
curl -k -d 'id=id1' --data-urlencode 'username=user1' --data-urlencode 'password=pass1' https://aaaa.bbb.com:nnnn
I was able to use
import requests
data = [
('id', 'id1'),
]
response = requests.post('https://aaaa.bbb.com/:nnnn', data=data)
With this I get certificate_verify_failed. How can I include urlencode for the username and password
For bypassing certification check use (verify=False) :
response = requests.post('https://aaaa.bbb.com/:nnnn', data=data, verify=False)
and for ensuring to send data as url encoded :
response = requests.post('https://aaaa.bbb.com/:nnnn', data=data, verify=False, headers={'Content-Type':'application/x-www-form-urlencoded'})
The reason for certificate_verify_failed is perhaps because the client is trying to verify the server certificate and failing in doing so. You may disable the verification on the client side by using verify=False
import requests
url = "https://aaaa.bbb.com:nnnn"
headers = {}
data = {
"id": "id1"
}
response = requests.post(url, data=data, headers=headers, verify=False)
Related
There is some api endpoint, which i try to send POST request with code like this
import requests
url = 'https://deviantart.com/api/v1/oauth2/collections/folders/create'
headers = {'Content-Type': 'application/json'}
data = {'folder': 'folder_name'}
params = {'access_token': '<authorization_code_flow_token>'}
r = requests.post(url=url,
headers=headers,
params=params,
json=data)
print(r.text)
But i get 400 response:
{
"error":"invalid_request",
"error_description":"Request field validation failed.",
"error_details":{"folder":"folder is required"},
"status":"error"
}
I don't understand why it fails, because their example with curl works fine.
curl https://www.deviantart.com/api/v1/oauth2/collections/folders/create \
-d "folder=Awesome Collection" \
-d access_token=Alph4num3r1ct0k3nv4lu3
And I was successful with post responses to get authenticated.
I tried to change content-type header(json and x-www-formurlencoded) and pass data-payload different ways(passing json string to data param, passing dict to json, passing paylod as query string). But It does not work. I dont have a clue what i am doing wrong. It seems like i send payload wrong or put wrong headers, but i tried a lot of "combinations" and still no effect.
For next hour you if you want try to help you can use access_token:
ba4550889c8c36c8d82093906145d9fd66775c959030d3d772
The following code will work.
import requests
url = "https://www.deviantart.com/api/v1/oauth2/collections/folders/create"
payload={'folder': 'Awesome Collection',
'access_token': 'ba4550889c8c36c8d82093906145d9fd66775c959030d3d772'}
files=[]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
I am trying to send a turtle file via a Python script using REST api to the local repository but the System is returning the following error
MALFORMED DATA: Illegal subject value: "-"^^http://www.w3.org/2001/XMLSchema#integer [line 1]
400
The used code is as follows:
import requests
url = 'http://localhost:7200/repositories/metaphactory1/statements'
with open("graph-29.ttl", "rb") as ttl_file:
file_dict = {"graph-29.ttl" : ttl_file}
headers = {
"Content-type": "application/x-turtle;charset=UTF-8",
}
r = requests.post(url, files=file_dict, headers=headers)
print(r.text)
print(r.status_code)
The same file when tried with a Curl command is working fine:
curl -X POST -H "Content-Type: application/x-turtle" -T graph-29.ttl 'http://localhost:7200/repositories/metaphactory1/statements'
Any idea regarding this issue is welcome
I think your problem come from the way you pass your file to the post request.
You should try using the data parameter of the post request. For example:
import requests
url = 'http://localhost:7200/repositories/metaphactory1/statements'
file_path = '/path/to/your/file/graph-29.ttl'
graph_name = 'http://graph-29'
headers = {
'Content-type': 'application/x-turtle',
}
params = {'graph': graph_name} #optional
response = requests.post(url, headers=headers, params=params, data=open(file_path,'r', encoding='utf-8').read())
curl --request POST -H "Content-Type: application/octet-stream" --data-binary "#/C:\\Users\\U6068366\\Downloads\\Koala.jpg" https://c6y09pww43.execute-api.us-east-1.amazonaws.com/p
--
App_Url = "https://p7a0km3l6k.execute-api.us-east-1.amazonaws.com/preprod/v1/images/trademark/metadata/providerPartition/{providerPartition}/providerPartitionId/{providerPartitionId}"
# f = open('C://Users//UX016491//PycharmProjects//DSSApi//data1.json')
# requests_json = json.loads(f.read())
files = {'media' : open('C:\\Users\\UX016491\\Desktop\\images\\image123.jpg','rb') }
response = requests.request("POST", App_Url, files = files, headers={"content-type": 'application/octet-stream'})
print(response)
if __name__ == '__main__':
test_createimage_data()
EDIT: I added url from python example because url in curl was incomplete. But it still need two values providerPartition and providerPartitionId
On https://curl.trillworks.com/ you can convert curl to python code. And mostly it works.
Here code from this page. But I can't test it.
import requests
# incompletet url from curl
#url = 'https://c6y09pww43.execute-api.us-east-1.amazonaws.com/p'
providerPartition = '??'
providerPartitionId = '??'
url = f'https://p7a0km3l6k.execute-api.us-east-1.amazonaws.com/preprod/v1/images/trademark/metadata/providerPartition/{providerPartition}/providerPartitionId/{providerPartitionId}'
headers = {
'Content-Type': 'application/octet-stream',
}
data = open('C:\\Users\\U6068366\\Downloads\\Koala.jpg', 'rb').read()
response = requests.post(url, headers=headers, data=data)
print(response.text)
You can also test both with url https://httpbin.org/post and it will send back what it get from you. And you can compare results from both requests. I tested curl and python code and I go the same information so they should give the same effect.
I am using requests to make a POST request to create a user. The request succeeds with 201 created when I use curl, however fails with a 500 response when I use requests. My curl command is
curl --user administrator:password -H "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d #user.json -v
And my python script is:
import requests
import json
headers = {
'Content-Type': 'application/json',
}
with open('user.json') as j:
data = json.load(j)
response = requests.post('https://localhost:8080/midpoint/ws/rest/users', headers=headers, data=str(data), auth=('Administrator', 'password'))
print(response)
Can anyone see a reason why my python script would be failing? I am at a loss.
str(data) returns the Python representation of data, not its JSON representation. These two forms can differ in things like ' vs. ", True vs. true, and None vs. null. To properly JSONify data, call json.dumps() on it:
response = requests.post(..., data=json.dumps(data))
or let requests do the JSONification:
response = requests.post(..., json=data)
or use the JSON as it appears in user.json directly:
with open('user.json') as j:
data = j.read()
response = requests.post(..., data=data)
I'm trying to replicate this cURL onliner:
curl -i -XPOST 'http://httpbin.org/post?db=data' --data-binary 'files,host=server10,folder=max value=0.64 1434055562121200000'
to python with the help of python-requests.
I've tried:
import requests
payload = {'files,host=server10,folder=max value=0.64 1434055562121200000':'\n'}
or payload = {'files,host':'server10,folder=max value=0.64 1434055562121200000'}
or {'files,host=server10,folder=max value=0.64 1434055562121200000':''}
or {'files,host=server10,folder=max value=0.64 1434055562121200000'}
r = requests.post('http://httpbin.org/post?db=data', data=payload)
I need some pointers or help to get past this snag, because it's possible right?
Answering my self.
import requests
url = 'http://10.10.10.10:8086/write?db=data'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = "cpu,host=server01,region=us-west value=0.64 1434055562000000000\n"
r = requests.post(url, data=payload, headers=headers)
With this, posting to influxdb via python works the same as the above cURL onliner.