Send curl POST Request in Python without installing a lib - python

I wanna send a curl post request in python. But I can't install any lib like 'request'. I could send POST request like following example :
curl -H "Content-Type: application/json" -X POST -d {\"username\":\"myusername\",\"password\":\"mypassword\"} https://example.com/login
I need equal code as the above in python2. Then, i must read what it returns. I'm working on Windows 10.

Write a curl command in a way that it can work in the shell and then execute the command in the shell.
This way you don't need the requests package.
import os
command = "curl -u {username}:{password} [URL]"
os.system(command)

Related

Execute curl command and fetch response

I have a curl command. I want to fetch the response after executing it in python 3.
curl https://api.abc.com/targets/targetID\
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: JWT PROBELY_AUTH_TOKEN"
How can I execute the script in Python and get the response ?
try this:
import requests
url = 'https://api.abc.com/targets/AxthamE0v3E-/scans/S6dOMPj8SsoH/'
r = requests.get(url,headers={"Content-Type": "application/json","Authorization": "JWT PROBELY_AUTH_TOKEN"})
print(r.text)
If you need to perform a request, you can use the library requests.
You can easily install it with pip install requests
https://www.w3schools.com/python/module_requests.asp
You can use this link to see how to work with this library

Curl command line to import xml file to Xray

Good afternoon,
I'm using robotframework to run some tests.
After I run them I have an output.xml file with the results.
I searched ways to import these results to Xray and found these links:
https://docs.getxray.app/display/XRAY/Testing+using+Robot+Framework+integration+in+Python+or+Java
https://docs.getxray.app/display/XRAY/Import+Execution+Results+-+REST#ImportExecutionResultsREST-RobotFrameworkXMLresults
So I created a .sh file with this command line:
#!/bin/bash
PROJECT=myproject
TESTPLAN=mytestplan
curl -X POST -H "Content-Type: multipart/form-data" -u myuser:mypassword -F "file=output.xml" "https://myserver/rest/raven/1.0/import/execution/robot?projectKey=$PROJECT&testPlanKey=$TESTPLAN"
It displays this error '' Forbidden (403)''.
Do you know how to solve this?
I guess you're using Xray on Jira server/Data Center and not Jira Cloud, correct?
Is so, it should be something like:
curl -H "Content-Type: multipart/form-data" -u admin:admin -F "file=#output.xml" "http://<jira_base_url>/rest/raven/1.0/import/execution/robot?projectKey=ROB&testPlanKey=ROB-12&testEnvironments=$BROWSER"
Note that sometimes <jira_base_url> is something like http://<some_ip>/jira .. is it your case perhaps?
Note: In this tutorial, you can find a concrete example for Xray on Jira server/DC. A similar tutorial for Xray on Jira Cloud can be found here.

how to use CURL commands in PYTHON

I want to send this command in a python program. How can I do this? I do not need to print any response.
curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'
Using os:
from os import system
system("curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'")
Using subprocess:
subprocess.Popen("curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'", shell=True)
You can use the shell module to run such commands neatly:
>>> from shell import shell
>>> curl = shell("curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'")
>>> curl.output()
Alternatively, I'd suggest using the requests module for making such http requests from Python.

Equivalent python code for curl command for https call

I have a curl command that works and gives me back the JSON.
Curl command:
curl -sS -k -L -H "Authorization: bearer <token>" -X GET https://IP:PORT/api/v1/namespaces
I tried with requestsand pycurl modules which I found in the other posts but no luck.
Can anyone help me with finding the equivalent in python???
We can do this with requests like this:
import requests
header = {'Authorization': 'bearer <token>'}
resp = requests.get("https://IP:PORT/api/v1/namespaces",
headers = header,
verify=False)
print(resp.status_code)
print(resp.text)
The -H switch behaviour is replicated by sending a header
The -L switch behaviour is replicated by specifying verify=False
The -sS and -k are about the behaviour of curl and not relevant here
The -X GET is replicated by using requests.get()

Using a python string output in curl

I have this python script
users=['mark','john','steve']
text=''
for user in users:
text+=str(user + " ")
print(text)
I want to output that string "text" into a curl terminal command.
I tried:
curl -d "#python-scirpt.py" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e
and
curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": 'python /home/scripts/python-script.py'}' https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e
or without the quotations in the text option
Everything returns this error
{"id":"Unable to parse incoming data","message":"Unable to parse incoming data","detailed_error":"","request_id":"fpnmfds8zifziyc85oe5eyf3pa","status_code":400}
How to approach this ? Any help is appreciated thank you.
another approach would be to curl inside python but would need help in that too.
Thank you
Use command substitution (i.e. $(...)) to make the shell run the python code first.
So
curl -d "$(python-scirpt.py)" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

Categories

Resources