How to feed custom options/flags in curl using python urllib2 library - python

I have to upload an artifact to JFrog Artifactory using the cUrl request
sample:
curl -sSf -H "X-JFrog-Art-Api:<API_KEY>" \
-X PUT \
-T file.zip \
'http(s)://<ARTIFACTORY_URL>/<REPO>/<PATH>/file.zip;released=true;build.number=1.0'
Now, I am using urllib2 (cannot use higher libraries like requests) and I understand the Header parts and data part , but the flags/options like -T -sSf, how to feed these in urllib?

Related

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

python - convert curl command

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)

Create a private repository on Docker Hub using Docker Registry API Client

I'm working on a Python 3.6 project in which I need to create a repository on the docker hub account by using API. I have googled a lot but couldn't find any API client to create repositories on docker hub account.
I have found only this module dockercloud from here and tried it in this way:
dockercloud.user = 'arycloud'
dockercloud.apikey = 'API_KEY'
print(client.Repository.list())
But it returns an error like this:
dockercloud.api.exceptions.AuthError: error getting credentials - err: exec: "docker-credential-osxkeychain": executable file not found in $PATH, out: ``
Is there any way to create a repo on Docker Hub using API?
The API documentation does not describe any mechanism for creating repositories on any registry, Docker Hub included.
However, I was able to create a repository using curl to send a POST request:
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/" \
--data 'description=test' \
--data 'full_description=full-description' \
--data 'is_private=false' \
--data 'name=test' \
--data "namespace=${UNAME}"

Convert cURL request to Python2 urllib2 request

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

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