python - convert curl command - python

I have this curl command.
This works
curl -O -H "X-Api:3433432" -X GET "https://website.com/file.zip"
Trying to figure it out how to convert it to something python understands.
curl -O -H "X-Api:3433432" -X GET "https://website/file.zip"

Perhaps you're looking for the "Requests" module?
It allows you to create a GET request in a similar way you would use a curl command.
You can find some documentation at:
https://www.w3schools.com/PYTHON/ref_requests_get.asp
Edit: There's also https://curlconverter.com/ which should convert it automatically for you.

try using module requests:
import requests
headers = {
'X-Api': '3433432',
}
response = requests.get('https://website.com/file.zip', headers=headers)

Related

Remove Docker images from privat repo with python

I need to run delete command on python for remove images from docker privat regestry. First of all i need to get correct digest sha56. I got from topic command for find digest.
I am using request library end. In terminal i use command
curl -u Username:Password -sS -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -o /dev/null -w '%header{Docker-Content-Digest}' https://regestry/v2/[repo]/manifests/[tag].
i try to use
headers={'Accept':'application/vnd.docker.distribution.manifest.v2+json' }
r=requests.get('https://registry/v2/[repo]/manifests/[tag]', auth=('USername', 'Password'))
print(r.headers['Docker-Content-Digest'])
But i got different digests and i can not delete final image.
Can you help me how i can use first command in python for execution.

How to send '-X POST' using 'requests' library in Python?

I am trying to replicate shell command:
curl -u [staff_email]:[api_key] -F "case[attachments][0]=#/path/to/file1.ext" -F "case[attachments][1]=#/path/to/file2.ext" -F "case[content]=I need help" -F "case[subject]=I need help" -F "case[user_email]=user#domain.ru" -F "case[user_full_name]=FullName" -F "case[language_id]=2" -F "case[custom_fields][cf_44]=3" -X POST https://[domain].omnidesk.ru/api/cases.json
into Python code using Requests library:
import requests
What is a proper syntax for this example (curl -X POST)?
You can use requests.post, as explained here
for the attachments part (your first -F), read here
for the auth part (-u): here

Extract Token from Curl Request

I have the following request:
curl --user world:hello http://127.0.0.1:9000/api/auth/
which returns following outpu:
{"status":"SUCCESS","token":"Mjg2NDA3NDUzMzY0MDgyMDc3NzM5NDg3MjcwODA4ODU5MTIyMjE0"}
How can I store token in a variable for later use? as below
'Content-Type': 'application/json'
'Token': 'TOKENadasfsdfsdfsdfsdsfgsfgfgfadfdgdf'
Python's json and subprocess libraries will help - just invoke the curl command from a Python subprocess, parse the resulting JSON, and save the result to a variable.
Since you are using cURL and asking for python solution I suggest passing that curl output to python and execute this code which decode input's json and print's the token:
curl --user world:hello http://127.0.0.1:9000/api/auth/ | python -c "import sys,json;print(json.load(sys.stdin)['token'])"

How to use curl command via python

I want to send a json file to keen.io in their documentation they use the following command
curl "https://api.keen.io/3.0/projects/PROJECT_ID/events/EVENT_COLLECTION?api_key=WRITE_KEY" -H "Content-Type: application/json" -d #purchase1.json
I am thinking how can I use this to work with python
you can use subprocess module to run the curl command using subprocess.call() method.
eg :
subprocess.call('curl "https://api.keen.io/3.0/projects/PROJECT_ID/events/EVENT_COLLECTION?api_key=WRITE_KEY" -H "Content-Type: application/json" -d #purchase1.json', shell=true)
You could use the requests library to achieve this.
import requests
uri = "https://api.keen.io/3.0/projects/{}/events/{}?api_key={}".format(PROJECT_ID, EVENT_COLLECTION, API_KEY)
json_payload = open('purchase1.json', 'rb').read()
requests.post(uri, json=json_payload)
You can read through their documentation for more information.
You can also use subprocess to execute the command and then catch the output https://docs.python.org/2/library/subprocess.html

How to upload a file to Atlassian Confluence page using curl

I am trying to upload an .xls file to a Confluence wiki page following the guidelines given in the Remote API documentation :
https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples#ConfluenceRESTAPIExamples-Uploadanattachment
curl -v -S -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=#myfile.txt" -F "comment=this is my file" "http://localhost:8080/confluence/rest/api/content/3604482/child/attachment" | python -mjson.tool
This is what I am doing:
curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "file=#/path/to/local/excelsheet.xls" https://<Confluence server>/display/page
I omitted the Python -mjson.tool as it says 'No JSON object could be decoded' and it didn't make sense as I am not posting JSON.
However the above curl command is not working for me. I see the html of the target page on my console and the file doesn't get uploaded. I tried modifying the curl command in several ways but nothing worked.
Also for the URL of the page I am trying to upload to, it doesn't have any contentID as suggested in the documentation. The target URL is a page which accepts attachments and shows the uploaded file list.
Could someone please point out where am I going wrong? I don't have much experience with Curl.
David Vonka answer is correct except for header "X-Atlassian-Token" value. It must be "no-check" (instead of "nocheck")
So corrected command is:
curl -v -S -X POST -H "X-Atlassian-Token: no-check" -F "file_0=#<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"
NOTE: replace all the <...> placeholders with your values
you need to use REST API in url: .../confluence/rest/api/content/$PAGE_ID/child/attachment and now you are using url of view page.
I do not think that confluence rest api allows file upload. Please do this instead
curl -v -S -X POST -H "X-Atlassian-Token: nocheck" -F "file_0=#<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"
replace all the <...> placeholders with your values
The API POST syntax should be corrected from "https://ConfluenceServer/display/page" to the correct res/api/content syntax eg: "https://companyName.atlassian.net/display/rest/api/content/pageIDxxxxx":
curl -v -S -X POST -H "X-Atlassian-Token: no-check" -F "file_0=#<file name>" -F "comment_0=<upload comment>" https://<companyName>.atlassian.net/display/rest/api/content/<pageID15398762>/child/attachment
The pageID can be found in the URL display page. For example:
https://companyName.atlassian.net/display/spaces/DEV/pages/pageID15398762/Page+Title+Name
For more API syntax details please refer to this documentation:
https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content-createContent

Categories