pythonpython-3.xapihttppython-requests

Python POST requests to an API with JSON data work on Requestbin, but not locally


I'm making API calls to a server with Python's Requests library. Locally, I have the JSON data in a directory, all files ending with .json. I am importing a JSON file to my Python file like so:

with open("./json_payloads/data.json") as user_file:
    json_data = user_file.read()

The type json_data is String and in JSON format. When I try the API POST request, I get back a 500 error from the server.

When doing it from Requestbin, I do everything exactly the same except instead of pulling JSON from a file, I have it in Requestbin's data store. I pull it like so:

  data_store = pd.inputs["data_store"]
  json_data = data_store["json_data"]

And I get back a 200 result from the server.

Any insights on why this is not working locally? I would much rather complete this locally for later steps in the process, like when I will upload files.


Solution

  • json_data read from the file is of type str. Convert it to a dict using json.loads before sending it with a request.

    import json
    json_data = json.loads(json_data)