Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API Request / JSON
#4
As you are using the requests library in Python to make a POST request to the NFIB SBET API. The 400 response you are getting indicates that there is likely an issue with the request you are sending to the API.

Data parameter should contain a JSON object with the request parameters. However, in your code, you are using the data parameter to send the JSON data. To properly send the JSON data, you should use the json parameter instead.

import requests
import json

url = "http://open.api.nfib-sbet.org/rest/sbetdb/_proc/getIndicators"
headers = {
    "Content-Type": "application/json",
    "app_name": "sbet"
}

data = {
    "params": [
        { "name": "minYear", "param_type": "IN", "value": 2010 },
        { "name": "minMonth", "param_type": "IN", "value": 6 },
        { "name": "maxYear", "param_type": "IN", "value": 2010 },
        { "name": "maxMonth", "param_type": "IN", "value": 12 },
        { "name": "indicator", "param_type": "IN", "value": "OPT_INDEX" }
    ]
}

response = requests.post(url, headers=headers, json=data)

print(response.status_code)
print(response.json())
By using the json parameter instead of the data parameter, the requests library will automatically set the Content-Type header to "application/json" and encode the JSON data correctly.
Reply


Messages In This Thread
API Request / JSON - by illmattic - May-09-2023, 01:45 PM
RE: API Request / JSON - by snippsat - May-10-2023, 07:47 AM
RE: API Request / JSON - by illmattic - May-10-2023, 12:52 PM
RE: API Request / JSON - by Gaurav_Kumar - Aug-01-2023, 10:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask rest api How to retrieve json request raysefo 4 6,197 Jan-20-2019, 06:46 PM
Last Post: raysefo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020