1

Trying to run a curl command to get the response and save this to a file

Have not been able to create code as this is first time attempting to use a curl command with python dont know where to begin

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "
{ \"Products\": [], \"DataProducts\": [], \"includeExtractFields\": true, \"includedDocumentTypes\": [], \"removeOrphans\": true, \"searchDataProduct\": \"Model|test\", \"searchField\": \"ID_TEST\", \"searchValues\": [ \"123456789\",\"987654321\" ] }
" "http://test"

The curl command should return json which is then saved to a file

3
  • 1
    Maybe this will help or possible dup - stackoverflow.com/questions/25491090/… Commented Jul 16, 2019 at 16:32
  • 1
    You could consider using post method from request library. a example. Commented Jul 16, 2019 at 16:34
  • on curl.trillworks.com you can convert curl to python's requests Commented Jul 16, 2019 at 16:44

1 Answer 1

5

You should consider doing this in native Python, rather than executing curl externally. Here's an example of how to make a POST request using Python and the requests package:

import requests
import json
response = requests.post('https://yoururl', data = {'key':'value'})
with open('output.json', 'w') as f:
    json.dump(response.json(), f)

You can read the requests documentation to read/write headers etc.

And for your specific case:

import requests
import json

headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}

data = {
    "Products": [],
    "DataProducts": [],
    "includeExtractFields": True,
    "includedDocumentTypes": [],
    "removeOrphans": True,
    "searchDataProduct": "Model|test",
    "searchField": "ID_TEST",
    "searchValues": [ "123456789","987654321" ]
}

# To send data form-encoded
response = requests.post('http://test/', headers=headers, data=data)

# To send data json-encoded
response = requests.post('http://test/', headers=headers, json=data)

# Save response as JSON file
with open('output.json', 'w') as f:
    json.dump(response.json(), f)
Sign up to request clarification or add additional context in comments.

4 Comments

This answer is correct and there's nothing wrong with using this method. Requests is a package that needs to be installed first though. You can achieve the same result by using urllib.request.urlretrieve which is part of the built-in urllib package. The code will be more verbose but it saves you a pip install.
The requests library is a full-function HTTP client library. It supports all HTTP verbs, including DELETE. See stackoverflow.com/questions/10191733/…
@Chase is there a DELETE function in urllib package
@SeánDempsey yes, you can use urllib.request.Request and set method='DELETE'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.