how to write json response of curl command to file - python

I was trying to write the result of curl to a file and extracting it and assign it to variable but i was not able to keep getting validations error
cmd='curl -k -u admin:admin -X POST --header "Content-Type:multipart/mixed"--header "Accept:application/json"-F "myPartnm1=#sftprest;type=application/json"-F" myPartname2=#sftppvteopenkey;type=application/json/oc-stream" http://cdrteo456.serms.com:4456/api/v/cert'
with open("dff.json","w") as f:
json.dump(os.system(cmd),ident=25)
with open("dff.json") as f1:
data= json.loads(f1)
print(data['id'])
Also attempted with requests:
import requests
headers = {
'Content-Type': 'multipart/mixed,
'Content-Type': 'multipart/mixed',
'Accept': 'application/json',
}
files = {
'myPartnm1': open('sftprest/opt/tr5.txt;type=application/json', 'rb'),
'myPartname2': open('sftppvteopenkeyopt/tr5/new.pem;type=application/json/oc-stream', 'rb'),
}
response = requests.post('http://cdrteo456.serms.com:4456/api/v/cert', headers=headers, files=files, verify=False, auth=('admin', 'admin'))
{
"massage":"Error validating request",
"validationError":["unable to import cerifecate.Expecting two body parts with certificate info and certificate info and certificate data"]
}

i was able to solve this by using .sh file extension in unix along with path file instead of curl

You need to add a space before the -F options
json"-F "myPartnm1=#sftp
json" -F "myPartnm1=#sftp
Add a space after the -F and remove the space after the double quote.
/json"-F" myPa
/json" -F "myPa
curl -k -u admin:admin -X POST --header "Content-Type:multipart/mixed" --header "Accept:application/json" -F "myPartnm1=#sftprest;type=application/json" -F "myPartname2=#sftppvteopenkey;type=application/json/oc-stream" http://cdrteo456.serms.com:4456/api/v/cert
I used https://curlconverter.com to convert your curl.
It did not convert until I made the above changes.

Related

Converting curl to python: HTTP 415 Unsupported Media Type

I'm trying to convert curl command to python script.
Here is the curl command which works great in command line:
curl -v -H 'X-Api-Key:123456789' https://synthetics.newrelic.com/synthetics/api/v3/monitors -G -d 'limit=100'
Here is the python script:
import requests
import json
headers = {
'X-Api-Key': '123456789'
}
data = {
'limit': '100'
}
response = requests.post(
'https://synthetics.newrelic.com/synthetics/api/v3/monitors', headers=headers, data=data)
print(response.content)
The output of the script is:
{"code":415,"message":"HTTP 415 Unsupported Media Type"}
I also tried to dump the data while passing it to the request using json.dump(data) which didn't help (Got the same error).
What am i doing wrong? Please advise.

convert curl to python3 and image to base64 not working

I have a curl command which is sending a jpg image converted to base64 to a web service:
curl -X POST --insecure \
https://link_to_the_web_service.com \
-H 'authorization:authorization_token' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{"model_spec": {"name": "inception", "signature_name": "predict_images"}, "inputs": {"images": {"dtype": 7, "tensor_shape": {"dim":[{"size": 1}]}, "string_val": ["IMAGE_CONVERTED_TO_BASE_64"]}}}'
I am converting the jpg image to base64 using the below website: "https://www.browserling.com/tools/image-to-base64". The execution of the curl command gives a success output.
Now I am testing the web service by taking jpg images, converting the curl command to python3 and the image to base64 using python3 like this:
import requests
import base64
host = 'https://link_to_the_web_service.com'
image = 'sample5.jpg'
image_64_encode = base64.b64encode(open('sample5.jpg',"rb").read())
headers = {'authorization': token, 'cache-control': 'no-cache', 'content-type': 'application/json'}
data={"model_spec": {"name": "inception", "signature_name": "predict_images"}, "inputs": {"images": {"dtype": 7, "tensor_shape": {"dim":[{"size": 1}]}, "string_val": [str(image_64_encode)]}}}
request = requests.post(url=host,
data=data,
headers=headers,
verify=False)
print(request)
and I receive a <500> response which means the web service is not able to read the image input.
I even tried 'base64.encodestring' to convert to base64 but no luck.
How do I properly convert the above curl command to python?
The data parameter of the requests.post method expects a string, not a dict. You should post your payload as JSON with the json parameter instead:
request = requests.post(url=host,
json=data,
headers=headers,
verify=False)

Formatting Header Dictionary for Requests in Python

I have the following format for request headers:
{
"projectName": New001,
"cloudRegions":{"REGION1":"centralus"},
"cloudAccountName":"XXX-XXXX-XXXX"
}
How do I format this to accept the {"REGION1":"centralus"}?
My Python code:
url = 'www.myexample.com'
headers = {'Content-Type': 'application/json',
'projectName': New001,
'cloudRegions':{'REGION1':'centralus'},
'cloudAccountName':'XXX-XXXX-XXXX'
}
r = requests.post(url, headers=headers)
The problem is I can't make the request to where cloudRegions will be formatted correctly. The value is in dictionary format but it doesn't like that. I've tried wrapping it in str(), using json.loads(), json.dumps(), but it always ends up formatted wrong. How do I format it to be an object that will be accepted as a pair?
This CURL works, and you will see the same format:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"projectName": "New001", \
"cloudRegions":{"REGION1":"centralus"}, \
"cloudAccountName":"XXX-XXXX-XXXX" \
}' 'http://www.myexample.com'
You are using HTTP headers to send your data (which is very unusual), while your curl example clearly shows that you must send the data in HTTP body, formatted as JSON. requests can do that very easily.
So simply use:
url = 'www.myexample.com'
data = {'projectName': 'New001',
'cloudRegions': {'REGION1':'centralus'},
'cloudAccountName': 'XXX-XXXX-XXXX'
}
r = requests.post(url, json=data)

Convert curl to Pycurl.

I have this curl command and I would like to know if I have converted it correctly to pycurl.
curl command
curl -D- -u fred:fred -X PUT --data{see below} -H "Content-Type:application/json" http://kelpie9:8081/rest/api/2/issue/QA-31
{
"fields":
{
"assignee":{"name":"harry"}
}
}
python code
def assign(self, key, name):
data = json.dumps({"fields":{"assignee":{"name":name}}})
c= pycurl.Curl()
c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.URL, "http://xxx/rest/api/2/issue/"+ key )
c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
c.setopt(pycurl.USERPWD, "****")
c.setopt(pycurl.PUT, 1)
c.setopt(pycurl.POSTFIELDS,data)
c.perform(
Nopes. First off: use curl's --libcurl option to get a first template.
Then, "-X PUT" translates to CUSTOMREQUEST set to "PUT", only changing the actual method keyword.
I would personally accomplish this by using the remarkable requests library
import requests
import requests.auth
import json
def assign(key, name):
url = "http://xxx/rest/api/2/issue/" + key
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
data = json.dumps({"fields": {"assignee": {"name": name}}})
r = requests.put(url, data=data, headers=headers, auth=requests.auth.HTTPBasicAuth('fred', 'fred'))
print(r.status_code)
print(r.json())
curl -j --libcurl git.txt -D- -u fred:fred -X PUT --data"{\"fields\":{\"assignee\":{\"name\":\"FRED\"}}}" -H "Content-Type:application/json" http://kelpie9:8081/rest/api/2/issue/QA-31
this gives the template in git.txt .
Also in the data field the inner quotes must be escaped as shown above.
The working code is attached below.
def assign(self, key, name):
self._startCurl()
self.c.setopt(pycurl.URL, "http://xxx/rest/api/2/issue/"+ key )
self.c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json'])
self.c.setopt(pycurl.USERPWD, "fred:fred")
self.c.setopt(pycurl.CUSTOMREQUEST, "PUT")
data = json.dumps({"fields":{"assignee":{"name":name}}})
self.c.setopt(pycurl.POSTFIELDS,data)
self.c.perform()
self.c.close()
Thanks to Daniel Stenberg for pointing it out

Execute curl command in python script

I want to implement this linux command in my PYTHON script, it returns a JSON file:
curl -k -u username:password -X POST https://servername/wapi/v1.2/network
/fkqjsdlmfjqslmdfsm:11.221.22.0/24/default?_function=next_availabl_ip -H
"Content-Type: application/json" -d '{"exclude": ["11.221.22.1",
"11.221.22.2", "11.221.22.3", "11.221.22.4", "11.221.22.5"], "num": 1}'
If you just want to execute this command using python, you can use
subprocess.call([<curl command>])
otherwise you might want to consider using http://pycurl.sourceforge.net/
I finally solved it with requests:
exclude_data = json.dumps({"exclude": ["11.221.22.1",
"11.221.22.2", "11.221.22.3", "11.221.22.4", "11.221.22.5"], "num": 1})
url = https://servername/wapi/v1.2/network
/fkqjsdlmfjqslmdfsm:11.221.22.0/24/default?_function=next_available_ip
r = requests.post(url, auth=(user, password), verify=False, data=exclude_data)

Categories

Resources