Iterate JSON And Post

A simple example how to read a JSON through stdin and iterate over a list and post each object to a REST endpoint.

#!/usr/bin/env python3

import requests
from requests.auth import HTTPBasicAuth
import json
import sys

# read -p "user: " jirauser; read -sp "pass: " jirapass; cat import.json | ./iterate_json_and_post.py $jirauser $jirapass http://devenv:8180/jira/rest/api/2/user

username = sys.argv[1]
password = sys.argv[2]
url = sys.argv[3]
data = json.load(sys.stdin)

for row in data:
    response = requests.post(
        url = url,
        headers = {"Content-type":"application/json"},
        json = row,
        auth = HTTPBasicAuth(username, password))
    print(response)

Reach out to me for professional support!

Contact