Can we consume an API, for example use the POST method to retrieve products. Note that we pass the security key in the body or data. it's possible ??
def api(request):
url = 'https://datausa.io/api/data?product'
x = requests.post(url,data={'securityKey':'valueKey')
contenu=x.text
return HttpResponse(contenu)
CodePudding user response:
Of course !
Most people use the requests library because it removes a lot of boilerplate and it is a really well built and maintained library : https://requests.readthedocs.io/en/latest/
It is as easy as doing that :
r = requests.get('https://datausa.io/api/data?product')
r.status_code
200
r.headers = {'security-key': 'valuekey'}
You can also use Django Rest Framework with the authentification mechanism so you can use token to consume your api : https://www.django-rest-framework.org/api-guide/authentication/
CodePudding user response:
J'ai une erreur HTTP 415 Voici le code
def api(request):
url = 'http://api-content/product/GetProduct'
x = requests.post(url, headers = {'security-key': 'valu-key'})
content=x.status_code
return HttpResponse(content)