chinskiy
chinskiy

Reputation: 2715

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.

Upvotes: 0

Views: 463

Answers (1)

J&#225;nos Farkas
J&#225;nos Farkas

Reputation: 471

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>"})

Upvotes: 1

Related Questions