Search code examples
pythondjangotastypie

How to make post request in Django TastyPie using ApiKeyAuthentication


I have resource like this:

class EntryResource(ModelResource):
    class Meta:
        queryset = Entry.objects.all()
        resource_name = 'entry'
        allowed_methods = ['post']
        authentication = ApiKeyAuthentication()
        authorization = Authorization()

And try to make request to this resource according to documentation:

requests.post('http://localhost/api/entry/',
              json={"key1": "value1",
                    "key2": "value2"},
              headers={"content-type": "application/json",
                       "Authorization": "ApiKey",
                       "<username>": "<api_key>"})

But get 401.


Solution

  • from documentation:

    Authorization: ApiKey daniel:204db7bcfafb2deb7506b89eb3b9b715b09905c8

    your request must be like this:

    requests.post('http://localhost/api/entry/',
                  json={"key1": "value1",
                        "key2": "value2"},
                  headers={"content-type": "application/json",
                           "Authorization": "ApiKey <username>:<api_key>"})