After opening the enter link description here I can see an API gets called which returns some data in JSON format. Attached you may see as well.
I am trying to call this API using my python script as follows.
JavaScript
x
25
25
1
payload={
2
"sort" : "tendersTotal.desc" ,
3
"filter" : {
4
"keyword" : {
5
"searchedText" : "" ,
6
"searchedFields" : []
7
} ,
8
"tenderLocationsOfActivity" : [] ,
9
"grantLocationsOfActivity" : [] ,
10
"grantSectorsOfActivity" : [] ,
11
"tenderSectorsOfActivity" : [] ,
12
"name" : "" ,
13
"types" : [] ,
14
"numberOfEmployees" : [] ,
15
"legalResidences" : []
16
} ,
17
"pageSize" : 1 ,
18
"pageNr" : 1
19
}
20
headers = {
21
'Content-Type' : 'application/json;charset=UTF-8'
22
}
23
r = requests.post("https://www.developmentaid.org/api/frontend/donors/search" , data=payload, headers=headers)
24
print(r.json())
25
but unfortunately, it returns me this message instead of the real data.
JavaScript
1
2
1
{"message":"Donor Search Request cannot be validated.","errors":{"pageNr":["The page nr field is required."],"pageSize":["The page size field is required."],"filter":["The filter field must be present."]}}
2
on the other hand, when I sent the same request using postman it returns me data. here is how I sent request using the postman.
my question is what changes do I need to bring in my python code so that the API calls correctly and returns me the data which I wish to receive.
Advertisement
Answer
Try using the json
parameter instead of data
:
JavaScript
1
2
1
r = requests.post("https://www.developmentaid.org/api/frontend/donors/search" , json=payload, headers=headers)
2
Output:
JavaScript
1
3
1
print(r.json())
2
{'data': {'total': 11573, 'meta': {'page_title': 'Search for donors — ', 'page_description': 'Search for donors and filter by legal residence, organization type, tender sectors of activity, tender countries of activity, grant sectors of activity and grant countries of activity.'}, 'items': [{'id': 118363, 'name': 'World Bank (USA)', 'shortName': 'WB', 'description': "<p><strong>WB - World Bank</strong> - is an international financial institution that provides loans to developing countries for capital programs. The World Bank's official goal is the reduction of poverty.</p><p>xa0</p><p>xa0</p>", 'avatar': 'https://www.developmentaid.org/files/organizationLogos/world-bank-118363.jpg', 'types': 'Financial Institution', 'legalResidence': 'USA', 'activeMember': False, 'slug': 'wb', 'jobsTotal': 1503, 'tenders_total': 117040, 'grants_total': 132, 'countryProgrammingTotal': 172, 'contractorsTotal': 28915}]}}
3