Is there a library/tool out there to convert raw application/json requests to curl command line (like show code in postman, or the curl in openapi).
Simple example under with just json:
---------------- request ----------------
POST https://xxxx.yyyy.zzzz/idpro-test-api/mid/business
User-Agent: python-requests/2.27.1
Accept-Encoding: gzip, deflate
accept: application/json
Connection: keep-alive
Content-Type: application/json
Content-Length: 86
{"user_id": "987000205", "ssn": "21010008287", "mid_issuer_id": 32, "activate": false}
---------------- response ----------------
<deleted example of a 500 I like to reproduce>
------------------------------------------
curl.exe in powershell (needs to escape the double-quotes):
curl.exe -vv -X POST -H "accept: application/json" -H "Content-Type: application/json" \
"https://xxxx.yyyy.zzzz/idpro-test-api/mid/business" \
-d '{\"user_id\": \"987000205\", \"ssn\": \"21010008287\", \"mid_issuer_id\": 32, \"activate\": false}'
curl in linux bash:
curl -vv -X POST -H "accept: application/json" -H "Content-Type: application/json" \
"https://xxxx.yyyy.zzzz/idpro-test-api/mid/business" \
-d '{"user_id": "987000205", "ssn": "21010008287", "mid_issuer_id": 32, "activate": false}'
I found curlify which is exactly what I want: https://github.com/ofw/curlify, (why cant pyCurl mention this on their site).
Related
I have a JOOMLA 3 system with Community Builder plugin. I need to update Users from a CSV file.
Here is the code I have tried:
if __name__ == "__main__":
s = fn_login()
r = s.get(cb_users)
print(r)
with open(IMPORT_FILE, 'rb') as f:
files = {'files': f}
r = s.post(post_url, files=files)
The session is ok. I have tried to figure out the POST message, but something may be missing. Nothing happens when I send the post. I have got response 200. What else do I need to consider?
Here is the request header:
curl 'https://3joomla.domain/administrator/index.php'
-X 'POST'
-H 'authority: 3joomla.domain'
-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 'accept-language: en-US,en;q=0.9,hu;q=0.8'
-H 'cache-control: max-age=0'
-H 'content-length: 6710'
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryALBLy00y6zolw7hC'
-H 'cookie: ....
-H 'origin: https://3joomla.domain'
-H 'referer: https://3joomla.domain/administrator/index.php'
-H 'sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"'
-H 'sec-ch-ua-mobile: ?0'
-H 'sec-ch-ua-platform: "Linux"'
-H 'sec-fetch-dest: document'
-H 'sec-fetch-mode: navigate'
-H 'sec-fetch-site: same-origin'
-H 'sec-fetch-user: ?1'
-H 'upgrade-insecure-requests: 1'
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
--compressed
This question already has answers here:
How to upload file with python requests?
(9 answers)
Closed 1 year ago.
I upload picture to imgbb.com site and show the curl command using firefox
curl "https://imgbb.com/json" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0" -H "Accept: application/json" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Content-Type: multipart/form-data; boundary=---------------------------270624834321652808912303071412" -H "Origin: https://imgbb.com" -H "Connection: keep-alive" -H "Referer: https://imgbb.com/" -H "Cookie: PHPSESSID=6o18hipgu8uk8orq4pg47of4b5; __gads=ID=40783678319bf866-227f347cf1a6003f:T=1614875864:RT=1614875864:S=ALNI_MbBjogRusDweg41MHFP01KawfJuiw" -H "DNT: 1" -H "Sec-GPC: 1" -H "TE: Trailers" --data-binary "-----------------------------270624834321652808912303071412"^
"Content-Disposition: form-data; name=""source""; filename=""test.png"""^
"Content-Type: image/png"^
""^
"-----------------------------270624834321652808912303071412"^
"Content-Disposition: form-data; name=""type"""^
""^
"file"^
"-----------------------------270624834321652808912303071412"^
"Content-Disposition: form-data; name=""action"""^
""^
"upload"^
"-----------------------------270624834321652808912303071412"^
"Content-Disposition: form-data; name=""timestamp"""^
""^
"1614875900988"^
"-----------------------------270624834321652808912303071412"^
"Content-Disposition: form-data; name=""auth_token"""^
""^
"e47e40461c19ef13a3768c45e2a0a5189f3bf625"^
"-----------------------------270624834321652808912303071412--"^
""
I can see test.png picture that I upload .
How can I send that command again using python with different picture?
You could use the online curl to Python requests generator.
But you should really be using the imgBB API instead.
I use requests as following:
headers = {
'Authorization': f'Bearer {self.__yandex_cloud_manager.get_iam_token().token}',
'Transfer-Encoding': 'chunked',
}
data = {
'voice': self.voice,
'emotion': 'neutral',
'lang': self.language,
'speed': self.temp,
'folderId': self.__get_folder_id(),
'format': 'oggopus',
'text': text,
}
response = requests.post(
url='https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize',
headers=headers,
data=data)
It sends this data:
send: b'POST /speech/v1/tts:synthesize HTTP/1.1\r\nHost: tts.api.cloud.yandex.net\r\nUser-Agent: python-requests/2.21.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Bearer <removed>\r\nTransfer-Encoding: chunked\r\nContent-Length: 135\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'voice=alyss&emotion=neutral&lang=ru-RU&speed=1.0&folderId=<removed>&format=oggopus&text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82'
And I get 400 response.
But when I dump request like this:
req = response.request
command = "curl -X {method} -H {headers} -d '{data}' '{uri}'"
method = req.method
uri = req.url
data = req.body
headers = ['"{0}: {1}"'.format(k, v) for k, v in req.headers.items()]
headers = " -H ".join(headers)
print(command.format(method=method, headers=headers, data=data, uri=uri))
It gives the following command:
curl -X POST -H "User-Agent: python-requests/2.21.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -H "Authorization: Bearer <removed>" -H "Transfer-Encoding: chunked" -H "Content-Length: 135" -H "Content-Type: application/x-www-form-urlencoded" -d 'voice=alyss&emotion=neutral&lang=ru-RU&speed=1.0&folderId=<removed>&format=oggopus&text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82' 'https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize'
And request with this command executes successfully.
What could be wroing?
Well, it works if I remove "Transfer-Encoding: chunked" header.
Working with a project, where using cookie for user identification.
When user arrives, it calls the service (which is running in localhost) and the service sending cookie with the response header looks like below:
curl 'http://127.0.0.1:8000/api/v1.0/tracking' -X OPTIONS -H 'Access-Control-Request-Method: POST' -H 'Origin: http://local.com:8080' -H 'Access-Control-Request-Headers: content-type,x-forwarded-for' --compressed
The response header looks like below:
HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: 60
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, x-forwarded-for
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PATCH, GET
Content-Length: 0
Content-Type: text/plain; charset=utf-8
Set-Cookie: id=random_id_123_123; expires=Wed, 06-Dec-2017 10:57:36 GMT; Domain=.local.com; Path=/
And then after a specific user action, the app is sending following API request:
curl 'http://127.0.0.1:8000/api/v1.0/tracking?event=video_added&user_id=123123123' -H 'Origin: http://local.com:8080' -H 'Accept: */*' -H 'Referer: http://local.com:8080/' -H 'Connection: keep-alive' --compressed
The request header for the above request looks like below:
GET api/v1.0/tracking?event=video_added&user_id=123123123 HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Accept: */*
Origin: http://local.com:8080
User-Agent: My user agent
Referer: http://local.com:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
I was expecting the cookie (random_id_123_123) to be received with the first request as response header would be the request header for the second request.
The website is running on: http://local.com:8080 (which actually running on local machine and my vhost config pointing 127.0.0.1 local.com) and its being served by python SimpleHTTPServer.
The backend service which is setting the cookie is running on port 8000 in localhost also.
Seems I have missed something during the implementation. Whats that?
Edit: Here is the code.
Your issue is that cookies are only sent based on the domain. Your code has
var settings = {
"crossDomain": true,
"url": "http://127.0.0.1:8000/api/v1.0/tracking?event=video_added&tracking_id=123123123",
"method": "GET",
}
The url is 127.0.0.1:8000 and it should be local.com:8000 if you want the cookies to be passed.
Last time I checked, curl doesn't have enabled the cookies by default.
To do so you will need to:
Use the parameter -b /path/to/cookiejar to read cookies.
Use the parameter -c /path/to/cookiejar to write cookies.
So your requests should become:
curl -c cookiejar 'http://127.0.0.1:8000/api/v1.0/tracking' \
-X OPTIONS -H 'Access-Control-Request-Method: POST' \
-H 'Origin: http://local.com:8080' \
-H 'Access-Control-Request-Headers: content-type,x-forwarded-for' \
--compressed
And:
curl -b cookiejar 'http://127.0.0.1:8000/api/v1.0/tracking?event=video_added&user_id=123123123' \
-H 'Origin: http://local.com:8080' \
-H 'Accept: */*' \
-H 'Referer: http://local.com:8080/' \
-H 'Connection: keep-alive' --compressed
I have a Python script using the Requests library that is of this form:
uhash = '1234567abcdefg'
cookies = {
'uhash':uhash
}
payload = {
'action':'trade.bump',
'hash':uhash,
'tradeid':'12345678'
}
r = requests.post(
'http://www.target_url.com/api/core',
cookies=cookies,
params=payload
)
Above is my Python attempt at creating the following cURL request (written with bash):
HASH="1234567abcdefg"
TRADEID="12345678"
curl 'http://www.target_url.com/api/core' -H "Cookie: uhash=$HASH" --data "action=trade.bump&hash=$HASH&tradeid=$TRADEID"
In summary, both scripts contain:
The cookie - uhash
Three data parameters called action, hash, and tradeid
My issue currently is, the bash script works - the server response for when I use the bash script is this:
{"meta":{"code":200},"data":{"bumped":true,"count":15}}
However, if I use the Python script, with the SAME cookie and parameter values as the bash script, I get:
{"meta":{"code":301},"data":{"message":"You can't bump a trade that doesn't exist ;_;"}}
The above error tells me the trade doesn't exist, despite that tradeid existing and the exact same one as my bash script's parameters.
I tried to debug using Firefox' convenient copy-as-curl tool to copy that curl command, which was how I made the bash script. However, once I tried to translate it to the Python script, it will tell me the aforementioned error. Maybe I am using the Requests library incorrectly, and I am missing something.
Attached is the full cURL request taken from Firefox (don't worry, the parameters were sanitized, meaning, they're not the real values):
curl 'http://www.tf2outpost.com/api/core' -H 'Host: www.tf2outpost.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:35.0) Gecko/20100101 Firefox/35.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: http://www.tf2outpost.com/trades' -H 'Cookie: __qca=P0-6517545-1420724809746; __utma=5135382.11011755.14224810.14331180.14180489.7; __utmz=51353782.1420724810.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); uhash=abcdefg12345678; mb_uid2=3211475230616776; CTag61=14338638870; __utmb=513532.9.10.14180489; __utmc=513782; __utmt=1; __utmt_b=1; __utmt_c=1; OX_plg=sl|qt|pm; HIRO_COOKIE=data=&newSession=true&id=2237524293×tamp=1433506185; HIRO_CLIENT_ID=67751187' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data 'action=trade.bump&hash=abcdefg12345678&tradeid=12345678'
Not quite sure why that is happening.
Try using data or json key instead of params, use json.dumps(payload) if data is your preferred method.