I am trying to convert a curl of pisignage into python requests. The curl is,
curl -X POST "https://swagger.piathome.com/api/files" -H "accept:
application/json" -H "x-access-token: login_session_token" -H
"Content-Type: multipart/form-data" -F "Upload
file=#test.jpg;type=image/jpeg"
My code is,
import requests
files = {'Upload file': open('test.jpg', 'rb'), 'type': 'image/jpeg'}
headers = {'Content-type': 'multipart/form-data', 'accept': 'application/json', 'x-access-token': 'login_session_token'}
file_response = requests.post(
'https://swagger.piathome.com/api/files',
files=files,
headers=headers
)
print(file_response)
It returns 404. I tried uncurl, the code is:
import uncurl
u = uncurl.parse('curl -X POST "https://swagger.piathome.com/api/files" -H "accept: application/json" -H "x-access-token: login_session_token" -H "Content-Type: multipart/form-data" -F "Upload file=test.jpg;type=image/jpeg"')
print(u)
The output is ,
error: unrecognized arguments: -F Upload file=test.jpg;type=image/jpeg
After a day of searching it turns out the swagger documentation is incorrect.
use
files = {
'assets': (open('test.jpg', 'rb'))
}
Try this
import requests
headers = {
'accept': 'application/json',
'x-access-token': 'login_session_token',
'Content-Type': 'multipart/form-data',
}
files = {
'Upload file': (None, 'test.jpg;type'),
}
response = requests.post('https://swagger.piathome.com/api/files', headers=headers, files=files)
link to parse curl to request python
Related
I'm using request module to send an http post request to a server which respond http 302 with the location redirect url "strangely" encoded:
'https:%3B//xxx.xxx.com?%3Bsrcext%3Dvalue&%3Berl=rrf
When i do the same request in chrome, chrome show me the location redirect URL as:
"https://xxx.xxx.com?srcext=value&erl=rrf"
Curl give me the same "response" as request module:
'https:%3B//xxx.xxx.com?%3Bsrcext%3Dvalue&%3Berl=rrf
Request options "followRedirect/followAllRedirects" fail to follow the link, curl option -L fail to follow the link.
No problem in chrome
I know i can apply "by hand" a method to decode the url "properly" but this is tricky as some parameters included need to stay encoded.
Is there an option in curl or request that i miss to directly "decode" the url and make the redirection option works ?
Thanks in advance
=========== EDIT =======
for request i'm using
request({
method: "POST",
url: "https://balbalab.com",
headers: {
'Cookie': "XXXXX",
'Accept-Encoding': 'gzip, deflate, fr',
'Accept-Language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/55.0.2883.87 Chrome/55.0.2883.87 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
"Pragma": "no-cache",
"Content-Type": "application/x-www-form-urlencoded"
},
gzip: true,
form: formFields,
followRedirect: true,
followAllRedirects: true
}
for curl i'm using:
curl -X POST "https://url1.url1.com" \
-H 'Accept-Encoding: gzip, deflate, fr' \
-H 'Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/55.0.2883.87 Chrome/55.0.2883.87 Safari/537.36' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H "Pragma: no-cache" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'xx=ff' \
-L
How do you parse the URL? It seems like you are missing url-encoded flag in your parser.
I have a curl command like below which works fine and I get the response back. I am posting json data to an endpoint which gives me response back after hitting it.
curl -v 'url' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: url' --data-binary '{"query":"\n{\n data(clientId: 1234, filters: [{key: \"o\", value: 100}], key: \"world\") {\n title\n type\n pottery {\n text\n pid\n href\n count\n resource\n }\n }\n}"}' --compressed
Now I am trying to read the binary data from temp.json file outside but somehow it doesn't work and I get an error -
curl -v 'url' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: url' --data-binary "#/Users/david/Downloads/temp.json" --compressed
I have stored json in below temp.json file -
{
data(clientId: 1234, filters: [{key: "o", value: 100}], key: "world") {
title
type
pottery {
text
pid
href
count
resource
}
}
}
This is the error I am getting -
.......
* upload completely sent off: 211 out of 211 bytes
< HTTP/1.1 500 Internal Server Error
< date: Fri, 28 May 2021 23:38:12 GMT
< server: envoy
< content-length: 0
< x-envoy-upstream-service-time: 1
<
* Connection #0 to host url left intact
* Closing connection 0
Is there anything wrong in my above curl command?
Update
If I copy the exact same content in the temp.json file that I have in my original curl with \n then it works fine. So looks like that is the issue.
It means I need to find a way to convert new lines to \n manually from temp.json before sending the curl request or is there any other way?
I've created a Bash script to get the data from the url using rest API from a appliance using username, password and saving the Session ID into a Variable and then using the session ID to get the data into csv format which is working fine.
I want to change the bash code into python3 code as i'm parsing it using pandas.
Bash Code:
#!/bin/bash
sessionID=$(curl -k -H "accept: application/json" -H "content-type: application/json" -H "x-api-version: 120" -d '{"userName":"administrator","password":"adminpass"}' -X POST https://hpe.sysnergy.com/rest/login-sessions | jq -r ".sessionID")
curl -k -H 'accept: application/json' \
-H 'content-type: text/csv' \
-H 'x-api-version: 2' \
-H "auth: $sessionID" \
-X GET https://hpe.sysnergy.com/rest/resource-alerts
Python Version of tries code:
#!/usr/bin/python3
import requests
import json
url = "https://hpe.sysnergy.com/rest/login-sessions"
data = {'username': 'administrator', 'password': 'adminpass'}
headers = {'Content-type': 'text/csv', 'Accept': 'application/json', 'x-api-version': 2}
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r)
I am getting below error:
Error:
requests.exceptions.InvalidHeader: Value for header {x-api-version: 2} must be of type str or bytes, not <class 'int'>
if i convert int to str as '2' then it gives another ssl error:
requests.exceptions.SSLError: HTTPSConnectionPool(host='hpe.synerg.com', port=443): Max retries exceeded with url: /rest/login-sessions (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:877)'),))
EDIT:
I have tried little different approach to get the same code format as bash in python but now it returns new error with new response code.
import os
import requests
sessionID = os.getenv('sessionID')
headers = {
'accept': 'application/json',
'content-type': 'text/csv',
'x-api-version': '2',
'auth': f"{sessionID}",
}
data = '{"userName":"administrator","password":"adminpassword"}'
response = requests.post('https://hpe.synergy.com/rest/login-sessions', headers=headers, data=data, verify=False)
print(response)
Error:
/python3/lib64/python3.6/site-packages/urllib3/connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'hpe.synergy.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning,
<Response [415]>
Please help or suggest the way to achieve same function in the python.
You first need to make a POST request to get the sessionID, then you need to make a GET request. Also note the headers are slightly different for the 2 requests. Something like this should work:
import requests
session = requests.Session()
url = "https://hpe.sysnergy.com/rest/login-sessions"
credentials = {"userName": "administrator", "password": "adminpass"}
headers = {"accept": "application/json",
"content-type": "application/json",
"x-api-version": "120",
}
response = session.post(url, headers=headers, json=credentials, verify=False)
session_id = response.json()["sessionID"]
url = "https://hpe.sysnergy.com/rest/resource-alerts"
headers = {"accept": "application/json",
"content-type": "text/csv",
"x-api-version": "2",
"auth": session_id,
}
response = session.get(url, headers=headers, verify=False)
print(response)
#print(response.content) # returns bytes
#print(response.text) # returns string
I wanna make a http request as shown in the below curl command:
curl -X PUT \
https://anypoint.mulesoft.com/cloudhub/api/v2/applications/highfiles \
-H 'authorization: Bearer XXX' \
-H 'cache-control: no-cache' \
-H 'content-length: 0' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'host: anypoint.mulesoft.com' \
-H 'postman-token: XXX' \
-H 'x-anypnt-env-id: XXX' \
-H 'x-anypnt-org-id: XXX' \
-F 'appInfoJson={
"muleVersion": {
"version": "3.8.5"
},
"properties":{"env":"dev"}
}'
I have tried the below request but all vain
files = {'file': open('C:\Users\\highfiles.zip', 'rb')}
appInfoJson1 = {
"muleVersion": {
"version": "3.8.5"
},
"properties": {"env":"dev1"}
}
print dict(appInfoJson=appInfoJson1)
headers = {"X-ANYPNT-ORG-ID": "XXXX",
"X-ANYPNT-ENV-ID": "XXXX",
"Authorization": "Bearer " + access_token,
}
response = requests.put("https://anypoint.mulesoft.com/cloudhub/api/v2/applications/highfiles",
data=dict(appInfoJson=appInfoJson1) , files=files, headers = headers)
How do I give a form-Param values and file in a python http request.
I was doing it wrong.
The change was only w.r.t handling the dict values, shown below:
response = requests.put("https://anypoint.mulesoft.com/cloudhub/api/v2/applications/highfiles",
data=dict(appInfoJson=appInfoJson1.values()) , files=files, headers = headers)
I am working with Spiceworks, a multipurpose IT tool and am trying to create a script to update our product keys in the database. I am not very experience with NodeJS so I am pretty confused how to create the equivalent to this curl command:
curl
'https://spiceworks.elogicgroup.com/software_licenses.js?device_view=true'
\
-H 'Cookie: spiceworks_session=BAh7F...9ffe' \
-H 'Origin: https://spiceworks.elogicgroup.com' \
-H 'Content-Type: multipart/form-data; boundary=----BOUNDARY' \
-H 'Cache-Control: max-age=0' \
--data-binary $'------BOUNDARY\r\nContent-Disposition:
form-data;name="authenticity_token"\r\n\r\ blfbEi...cK+/k=
\r\n------BOUNDARY\r\nContent-Disposition: form-data;
name="_pickaxe"\r\n\r\n\u2e15\r\n------BOUNDARY\r\nContent-Disposition:
form-data; name="software_license[software_id]"\r\n\r\n
513\r\n------BOUNDARY\r\nContent-Disposition: form-data; name="software_installation"\r\n\r\n
3854\r\n------BOUNDARY\r\nContent-Disposition: form-data; name="software_license[type]"\r\n\r\nIndividualLicense\r\n------BOUNDARY\r\nContent-Disposition:
form-data; name="software_license[key]"\r\n\r\n
54321\r\n------BOUNDARY--\r\n'
BAh7F...9ffe is your SpiceWorks session cookie.
blfbEi...cK+/k is an authentication token.
513 is the ID of the software application
3854 ties the software application to a specific install on a specific computer
I would really appreciate any tips or directions you all could point me in, I am really struggling with this.
Thanks!!
Jack
Using https://github.com/mikeal/request
var r = request.post({
url: 'https://spiceworks.elogicgroup.com/software_licenses.js?device_view=true',
headers: {
'Cookie': 'spiceworks_session=BAh7F...9ffe',
'Origin': 'https://spiceworks.elogicgroup.com/',
'Cache-Control': 'max-age=0'
}
});
var form = r.form();
form.append('authenticity_token', 'blfbEi...cK+/k');
. . .