Skip to content
Advertisement

can’t get the correct data by calling an API using python request library while it works with postman

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.

enter image description here I am trying to call this API using my python script as follows.

payload={
    "sort" : "tendersTotal.desc" ,
    "filter" : {
        "keyword" : {
            "searchedText" : "" ,
            "searchedFields" : []
            } ,
        "tenderLocationsOfActivity" : [] ,
        "grantLocationsOfActivity" : [] ,
        "grantSectorsOfActivity" : [] ,
        "tenderSectorsOfActivity" : [] ,
        "name" : "" ,
        "types" : [] ,
        "numberOfEmployees" : [] ,
        "legalResidences" : []
        } ,
    "pageSize" : 1 ,
    "pageNr" : 1
    }
headers = {
    'Content-Type' : 'application/json;charset=UTF-8'
    }
r = requests.post("https://www.developmentaid.org/api/frontend/donors/search" , data=payload, headers=headers)
print(r.json())

but unfortunately, it returns me this message instead of the real data.

{"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."]}}

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.

enter image description here

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:

r = requests.post("https://www.developmentaid.org/api/frontend/donors/search" , json=payload, headers=headers)

Output:

print(r.json())
{'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}]}}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement