Home > Net >  how to write json response of curl command to file
how to write json response of curl command to file

Time:10-07

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"]
}

CodePudding user response:

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

CodePudding user response:

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