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

Seán Dempsey
  • 105
  • 3
  • 10
  • 1
    Maybe this will help or possible dup - https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command – DaveStSomeWhere Jul 16 '19 at 16:32
  • 1
    You could consider using post method from request library. [a example](https://stackoverflow.com/questions/11322430/how-to-send-post-request). – Tupio Jul 16 '19 at 16:34
  • on https://curl.trillworks.com/ you can convert `curl` to python's `requests` – furas Jul 16 '19 at 16:44

1 Answers1

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)
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • 2
    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](https://docs.python.org/3/library/urllib.request.html#legacy-interface) which is part of the built-in [urllib](https://docs.python.org/3/library/urllib.html) package. The code will be more verbose but it saves you a pip install. – Chase Jul 16 '19 at 21:07
  • 1
    The requests library is a full-function HTTP client library. It supports all HTTP verbs, including DELETE. See https://stackoverflow.com/questions/10191733/how-to-do-a-http-delete-request-with-requests-library – jarmod Jul 17 '19 at 14:36
  • @Chase is there a DELETE function in urllib package – Seán Dempsey Jul 17 '19 at 14:41
  • 1
    @SeánDempsey yes, you can use [urllib.request.Request](https://docs.python.org/3/library/urllib.request.html#urllib.request.Request) and set `method='DELETE'`. – Chase Jul 18 '19 at 00:16