Python GET Request returns data when tried on Postman but the generated python code not working

46 Views Asked by At

I have this URL that works on Postman and returns data:

https://www.tablebuilder.singstat.gov.sg/api/table/resourceid?isTestApi=true&keyword=manufacturing&searchoption=all

But the Python code generated on Postman does not work.

import requests
url = "https://www.tablebuilder.singstat.gov.sg/api/table/resourceid?isTestApi=true&keyword=manufacturing&searchoption=all"
response = requests.request("GET", url)
print(response.text)

What could the reason be? This code used to work in the past. Is there a permanent fix for the problem?

1

There are 1 best solutions below

2
SIGHUP On BEST ANSWER

You need to provide a User-Agent HTTP header.

For example:

import requests
import json

url = "https://www.tablebuilder.singstat.gov.sg/api/table/resourceid"
params = {
    "isTestApi": "true",
    "keyword": "manufacturing",
    "searchoption": "all"
}
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.15"
}

with requests.get(url, params=params, headers=headers) as response:
    response.raise_for_status()
    print(json.dumps(response.json(), indent=2))