POST request with a JSON file
Use this Python script to make a POST request to certain V‑Spark endpoints.
#!/usr/bin/env python
# Unsupported example code - Not for production use.
import sys
import json
import requests
# default values
PROTOCOL = "http://"
PORT = "3000"
if ( len(sys.argv) != 5 ):
print " Usage:", sys.argv[0], "HOST ROOT_TOKEN API_TO_CALL INPUT_JSON_FILE"
sys.exit(-1)
else:
HOST, ROOT_TOKEN, API_TO_CALL, INPUT_JSON_FILE = sys.argv[1:]
# Define the URL in a single variable for JSON load
url = "%s%s:%s%s?token=%s" % (PROTOCOL, HOST, PORT, API_TO_CALL, ROOT_TOKEN)
print "POSTing data from " + INPUT_JSON_FILE + " to" + API_TO_CALL
with open(INPUT_JSON_FILE) as json_file:
json_data = json.load(json_file)
headers = {'Content-type': 'application/json'}
response = requests.post(url, headers=headers, data=json.dumps(json_data))
if response.status_code != 200:
print ' HTTP message: ', response.reason, ' HTTP return code: ', str(response.status_code)Use the script with the following command and arguments:
python api_post.py $host $token $endpoint $json- $host
- Hostname or URL for the system running V‑Spark.
- $token
-
Authorization token with the required read or write permissions for the request.
- $endpoint
- Request endpoint, including any path parameters.
- $json
- Path to the request's JSON file.
