The Urllib.request Returns Empty Data, While The Same Request In Postman Returns Correct Data
My url: https://www.grants.gov/grantsws/rest/opportunities/search/ url payload: payload = { 'startRecordNum':0, 'sortBy':'openDate|desc', 'oppStatuses':'fo
Solution 1:
You need to encode the payload in json format using json.dumps(payload)
:
import urllib.request
import json
req = urllib.request.Request('https://www.grants.gov/grantsws/rest/opportunities/search/')
req.add_header('Content-Type','application/json; charset=UTF-8')
payload = {
"startRecordNum": 0,
"sortBy":"openDate|desc",
"oppStatuses":"forecasted|posted"
}
data = json.dumps(payload).encode()
with urllib.request.urlopen(req, data) as response:
dataList = json.load(response)
print(dataList)
Post a Comment for "The Urllib.request Returns Empty Data, While The Same Request In Postman Returns Correct Data"