Skip to content

Instantly share code, notes, and snippets.

@sshaaf
Created November 30, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshaaf/eee6f510a3da5101bc94039d3c195955 to your computer and use it in GitHub Desktop.
Save sshaaf/eee6f510a3da5101bc94039d3c195955 to your computer and use it in GitHub Desktop.
Loads a mock json file(not array), reads line by line, and send it to server on a particular end-point
# python request json data loaded from a file
import requests
# reads 1 line at a time, this is not an array
file1 = open('MOCK_DATA.json', 'r')
Lines = file1.readlines()
header = {
'Content-Type': 'application/json'
}
for line in Lines:
URL = "http://localhost:8080/products"
r = requests.post(URL, data=line.strip(), headers=header)
print("Status code: ", r.status_code)
{"code":"Econoline E150","name":"Ford","price":91,"stock":97}
{"code":"F350","name":"Ford","price":61,"stock":27}
{"code":"Fit","name":"Honda","price":42,"stock":65}
{"code":"Tiguan","name":"Volkswagen","price":91,"stock":31}
{"code":"Galant","name":"Mitsubishi","price":10,"stock":12}
{"code":"Sable","name":"Mercury","price":43,"stock":40}
{"code":"Bonneville","name":"Pontiac","price":88,"stock":9}
{"code":"Transit Connect","name":"Ford","price":63,"stock":87}
{"code":"Equus","name":"Hyundai","price":30,"stock":54}
{"code":"DeVille","name":"Cadillac","price":55,"stock":90}
{"code":"Spider","name":"Alfa Romeo","price":65,"stock":52}
{"code":"Firebird","name":"Pontiac","price":77,"stock":4}
{"code":"Ram 2500 Club","name":"Dodge","price":61,"stock":62}
{"code":"Range Rover","name":"Land Rover","price":34,"stock":36}
{"code":"GX","name":"Lexus","price":87,"stock":89}
{"code":"1500 Club Coupe","name":"GMC","price":83,"stock":50}
{"code":"Firebird","name":"Pontiac","price":61,"stock":77}
{"code":"Savana 3500","name":"GMC","price":96,"stock":24}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment