I am trying to convert the below code in curl to python2 urllib2 request call for performing a "PUT" operation, it works fine with cURL but i am clueless on getting this converted to python2 code.
cURL
curl -X PUT -u myUser:myPassword -T test.txt “http://localhost:8081/artifactory/libs-release-local/test/test.txt”
The problem i face is with the "-T" flag i am not sure how set this in urllib2 call. I tried using Postman for help by adding file to Body->form-data->file
But the code is converted as below which i suspect is not the transfer file call.
curl -X PUT \
http://localhost:8081/artifactory/my-repository/my/new/artifact/directory/file.txt \
-H 'Cache-Control: no-cache' \
-H 'Postman-Token: 4ffbfca4-a4be-09de-c8ab-0731a2d67009' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F '=#C:\Devops\file.txt'
I tried looking at documentations specified here under -T/--upload-file it does not help. Any suggestions.
https://www.computerhope.com/unix/curl.html
https://curl.haxx.se/libcurl/c/options-in-examples.html
Related
I'm trying to execute some curl POST commands to a SSH server using Paramiko.
The problem is that the curl command includes quotes and simple quotes and some extra characters are added when sending the command to the server.
What I try to send is the following:
ssh.exec_command("curl -X POST -H 'Content-Type: application/json' -d '{\"backlight\":true}' [127.0.0.1]")
Server answers with invalid JSON. I checked it and what reaches the server is:
curl -X POST -H \'Content-Type: application/json\' -d \'{"backlight":true}\' [127.0.0.1]
There are some backslashes added to my string, so that the curl command is invalid.
I tried a lot of things like escaping, replace(), change the command etc. nothing worked.
Do you have any tips or ideas how to solve this issue? It's really frustrating.
Server command should look like this:
curl -X POST -H 'Content-Type: application/json' -d '{"backlight":true}' [127.0.0.1]
We currently have a cURL request like below and we'd like to convert it into Python. How do I do this? From my googling, all I am seeing is how to send files but not a folder files. Thank you!
curl
--location
--request PUT 'https://some-url.com/files' \
--header 'Content-Type: application/octet-stream' \
--data-binary '#/path/to/files'
I have similar apps on Flask and FastAPI.
When I do this curl requests with Flask, that is all right:
Without TLS:
curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' http://X.X.X.X:5050/
{"error":0,"result":{"token":"XXX"}}
With TLS:
curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' https://example.com:8443/api/
{"error":0,"result":{"token":"XXX"}}
!!! But with FastAPI I get another result:
Without TLS:
curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' http://X.X.X.X:5050/
{"error":0,"result":{"token":"XXX"}}
With TLS:
curl -X POST -H "Content-Type: application/json" -d '{"method": "account.auth"}' https://example.com:8443/api/
Unsupported upgrade request.
How to fix problem with "Unsupported upgrade request."? And what is it? Flask are working with it normally.
This same issue usually seems to arise from incomplete uvicorn installations, but is usually related to websockets.
A solution for this issue might be to reinstall uvicorn with the recommended (by FastAPI) extras:
python3 -m pip uninstall uvicorn
python3 -m pip install uvicorn[standard]
I had this problem using java to access the api. The solution was to set HTTP Request to 1.1
var httpRequest = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:8000/jobs"))
.version(HttpClient.Version.HTTP_1_1)
.GET()
.build();
curl -u username:password -X POST -H "Content-Type: application/json" -d #test.json http://x.x.x.x:111/vs
tried to run the above using
os.system(curl -u username:password -X POST -H "Content-Type: application/json" -d #test.json http://x.x.x.x:111/vs)
getting "bad request" as response
how can i implement this using pycurl?
or is there any other subprocess that i can use,
I am working on windows
I'd do that in this way if I were you:
curl_cmd = '''
curl -u username:password -X POST -H "Content-Type: application/json" -d #test.json http://x.x.x.x:111/vs
'''
os.system(curl_cmd)
If you want to use pycurl, the you can see the example of how to post data using pycurl. Along with this post data you'll need to add authentication (user:pass) and you can do is in this way:
userAndPass = b64encode(b"username:password").decode("ascii")
pycurl.setopt(pycurl.HTTPHEADER, ['Authorization' : 'Basic %s' % userAndPass])
Python newb here, here's my current script:
#!/usr/bin/env python
import os
import time
import datetime
import subprocess
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime(%Y%m%d)
My curl command:
curl -i -k -H -o <timestamp>.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:******" -H "appKey:*******" -X GET https://*****.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=******&term_from=<timestamp>
The dynamic aspect of this curl request comes from the python portion of my script. I need the output file to be the $currentTime.txt and i need the php variable $term_from to also be the timestamp.
So far ive tried invoking the curl command using
os.system('curl -i -k -H -o' + %st + '.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:arcsght_app" -H "appKey:*****" -X GET https://csopen.teamaol.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=Dulles&term_from=' + %st)
That didn't work, then i tried using
subprocess.call(<same curl command as above>)
and that didnt work.
Ive tried my curl command from bash and it works, and i can get my timestamp to show how i need it. I just cant figure out how to tie everything together. Before i posted this i did try to figure it out on my own, but this is my first real adventure into python so my knowledge of what works and what doesn't is pretty slim. Looking for help! Thanks.
my_command = 'curl -i -k -H -o {timestamp}.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:******" -H "appKey:*******" -X GET https://*****.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=******&term_from={timestamp}'.format(timestamp=123456)
os.system(my_command)
should work fine ... Im not entirely sure why you want to do this in python... but this should allow you to no problem