Is it possible to loop through a JSON file using shell to call to an API?
Question:
Wondering if it is possible to take a json file such as:
[
{
"firstName": "John",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "12345",
"subscriberId": "123456789",
"policyNumber": "12345"
},
{
"firstName": "Jane",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "54321",
"subscriberId": "987654321",
"policyNumber": "54321"
}
]
& looping over that data to call to the API.
The shell script that I’m using to make one call to the API looks like this:
#!/bin/bash
# Rally Forwarder Keys
clientkey=test
apikey=test
#SET apikey=test
# Public URL
url="test"
curl -v -X POST -d '{"firstName":"test","lastName":"test","dob":"1900-01-01","zipCode":"12345","subscriberId":"123456789","policyNumber":"12345"}' -H "Content-type:application/json" "${url}" -H "X-OnePass-API-Key: ${apikey}" -H "X-OnePass-ClientKey: ${clientkey}"
I run a check for eligible members and have a list of about 700 that I really don’t want to copy & paste and then manually edit the data for each call.
My original code is in VBA Selenium which does not work with the API. It originally went to a website, used the data to fill in a form and submitted the form to grab the code. My shell scripting skills are still pretty basic or would python work better for this?
Answers:
Well use json
and requests
packages as follows :
import json
import requests
# Setup
API_KEY = ...
CLIENT_KEY = ...
URL = ...
HEADERS = {
'Content-type': 'application/json',
'X-OnePass-API-Key': API_KEY,
'X-OnePass-ClientKey': CLIENT_KEY,
}
f = open('data.json')
# returns JSON object as
# an array
data = json.load(f)
# Iterating through the json
for d in data:
# Send HTTP request
response = requests.post(URL, headers=headers, json=d)
# Do whatever
...
I’ve never implemnted & tested this. You can simply inject the data before runing using fstrings
in python. & run curl uisng os.sytem
as follows.Try this method pls, Let me know if it deosn’t works.
import os
l = [
{
"firstName": "John",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "12345",
"subscriberId": "123456789",
"policyNumber": "12345"
},
{
"firstName": "Jane",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "54321",
"subscriberId": "987654321",
"policyNumber": "54321"
}
]
# Rally Forwarder Keys
clientkey="test"
apikey="test"
#SET apikey=test
# Public URL
url="test"
for x in l:
injection =f'curl -v -X POST -d "{x}" -H "Content-type:application/json" "${url}" -H "X-OnePass-API-Key: ${apikey}" -H "X-OnePass-ClientKey: ${clientkey}"'
os.system(injection)
After injection your curl looks like this.
curl -v -X POST -d "{‘firstName’: ‘John’, ‘lastName’: ‘Doe’, ‘dob’:
‘1900-01-01’, ‘zipCode’: ‘12345’, ‘subscriberId’: ‘123456789’,
‘policyNumber’: ‘12345’}" -H "Content-type:application/json" "$test"
-H "X-OnePass-API-Key: $test" -H "X-OnePass-ClientKey: $test"
curl -v -X POST -d "{‘firstName’: ‘Jane’, ‘lastName’: ‘Doe’, ‘dob’:
‘1900-01-01’, ‘zipCode’: ‘54321’, ‘subscriberId’: ‘987654321’,
‘policyNumber’: ‘54321’}" -H "Content-type:application/json" "$test"
-H "X-OnePass-API-Key: $test" -H "X-OnePass-ClientKey: $test"
…… ……
Wondering if it is possible to take a json file such as:
[
{
"firstName": "John",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "12345",
"subscriberId": "123456789",
"policyNumber": "12345"
},
{
"firstName": "Jane",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "54321",
"subscriberId": "987654321",
"policyNumber": "54321"
}
]
& looping over that data to call to the API.
The shell script that I’m using to make one call to the API looks like this:
#!/bin/bash
# Rally Forwarder Keys
clientkey=test
apikey=test
#SET apikey=test
# Public URL
url="test"
curl -v -X POST -d '{"firstName":"test","lastName":"test","dob":"1900-01-01","zipCode":"12345","subscriberId":"123456789","policyNumber":"12345"}' -H "Content-type:application/json" "${url}" -H "X-OnePass-API-Key: ${apikey}" -H "X-OnePass-ClientKey: ${clientkey}"
I run a check for eligible members and have a list of about 700 that I really don’t want to copy & paste and then manually edit the data for each call.
My original code is in VBA Selenium which does not work with the API. It originally went to a website, used the data to fill in a form and submitted the form to grab the code. My shell scripting skills are still pretty basic or would python work better for this?
Well use json
and requests
packages as follows :
import json
import requests
# Setup
API_KEY = ...
CLIENT_KEY = ...
URL = ...
HEADERS = {
'Content-type': 'application/json',
'X-OnePass-API-Key': API_KEY,
'X-OnePass-ClientKey': CLIENT_KEY,
}
f = open('data.json')
# returns JSON object as
# an array
data = json.load(f)
# Iterating through the json
for d in data:
# Send HTTP request
response = requests.post(URL, headers=headers, json=d)
# Do whatever
...
I’ve never implemnted & tested this. You can simply inject the data before runing using fstrings
in python. & run curl uisng os.sytem
as follows.Try this method pls, Let me know if it deosn’t works.
import os
l = [
{
"firstName": "John",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "12345",
"subscriberId": "123456789",
"policyNumber": "12345"
},
{
"firstName": "Jane",
"lastName": "Doe",
"dob": "1900-01-01",
"zipCode": "54321",
"subscriberId": "987654321",
"policyNumber": "54321"
}
]
# Rally Forwarder Keys
clientkey="test"
apikey="test"
#SET apikey=test
# Public URL
url="test"
for x in l:
injection =f'curl -v -X POST -d "{x}" -H "Content-type:application/json" "${url}" -H "X-OnePass-API-Key: ${apikey}" -H "X-OnePass-ClientKey: ${clientkey}"'
os.system(injection)
After injection your curl looks like this.
curl -v -X POST -d "{‘firstName’: ‘John’, ‘lastName’: ‘Doe’, ‘dob’:
‘1900-01-01’, ‘zipCode’: ‘12345’, ‘subscriberId’: ‘123456789’,
‘policyNumber’: ‘12345’}" -H "Content-type:application/json" "$test"
-H "X-OnePass-API-Key: $test" -H "X-OnePass-ClientKey: $test"curl -v -X POST -d "{‘firstName’: ‘Jane’, ‘lastName’: ‘Doe’, ‘dob’:
‘1900-01-01’, ‘zipCode’: ‘54321’, ‘subscriberId’: ‘987654321’,
‘policyNumber’: ‘54321’}" -H "Content-type:application/json" "$test"
-H "X-OnePass-API-Key: $test" -H "X-OnePass-ClientKey: $test"…… ……