Transformaing CURL post request to python request - python

I am running a CURL command and want to update it to python request command.
CURL Command:
curl -H "Content-Type: application/json" -X POST -k -s --date '{"command": "/semate:severitytoAll', "target": "/gen1/gateway[(#name=\"#gname\")]/directory/probe[(#name=\"#probn\")]/managedE[(#name=\"#me\")]/sam[#name=\"PRO")] [(#type=\"\')]/dav[(#name=\"PRO\")]/row/rows[(#name=\""PRO_D\')]", "args': {"1": "testing sn via Rest aPI", '2": 1, "4":3}}' -u #gatName
Can someone guide me with the equivalent python request commands please
Regards
Bicky

check https://www.postman.com/product/rest-client/
Write it in Postman, select code and select Python.

Related

Problem with sending JSON POSTs using paramiko

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]

FastAPI (Python) Why I get "Unsupported upgrade request." with POST request?

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();

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 convert the curl command to implement it in pycurl? or any alternative to os.system

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])

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