cURL request to Python loading json file
-
Good day, everyone! Piton's not strong, but it's a challenge, and it needs to be solved.
There's a smoke like this:
curl -viL --basic --user <AccessKey>:<SecretKey> \ -H "Accept: application/json" -d "@request.json" \ -POST "<EndPointURL>"
The command line is being successfully implemented. But there's something to do with it in the python, and I don't really know how to do it. A little diagnosed the requests, but 403 errors returned when requested.
auth = (self.access_key, self.secret_key)
headers = {'Accept': 'application/json'} files = {'document': open(json_file, 'rb')} r = requests.post(method_url, files=files, auth=auth, headers=headers)
Send it to the right side. Could such a request be made through requests? Or do you have to dig some kind of pycURL? Maybe someone's got some examples of the code?
UPDI tried to pretend to be a browser, it didn't work, it was the same thing.
auth = (self.access_key, self.secret_key)
headers = {'Accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (K HTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'} files = {'document': open(json_file, 'rb')} r = requests.post(method_url, files=files, auth=auth, headers=headers)
-
Problem solved. I messed up, and I passed the file through the files. Specifically, in my case, the file is transferred to d:
-d "@request.json"
I mean, in the requests, he had to go straight to the data parameter. Final work code:
auth = (self.access_key, self.secret_key)
headers = { 'Accept': 'application/json' } data = open(json_file, 'rb') r = requests.post(method_url, data=data, auth=auth, headers=headers)