Django Rest Framework - Authentication credentials were not provided

前端 未结 12 1693
野性不改
野性不改 2020-12-04 07:12

I\'m developing an API using Django Rest Framework. I\'m trying to list or create an \"Order\" object, but when i\'m trying to access the console gives me this error:

<
12条回答
  •  生来不讨喜
    2020-12-04 08:16

    If you are playing around in the command line (using curl, or HTTPie etc) you can use BasicAuthentication to test/user your API

        REST_FRAMEWORK = {
            'DEFAULT_PERMISSION_CLASSES': [
                'rest_framework.permissions.IsAuthenticated',
            ],
            'DEFAULT_AUTHENTICATION_CLASSES': (
                'rest_framework.authentication.BasicAuthentication',  # enables simple command line authentication
                'rest_framework.authentication.SessionAuthentication',
                'rest_framework.authentication.TokenAuthentication',
            )
        }
    

    You can then use curl

    curl --user user:password -X POST http://example.com/path/ --data "some_field=some data"
    

    or httpie (its easier on the eyes):

    http -a user:password POST http://example.com/path/ some_field="some data"
    

    or something else like Advanced Rest Client (ARC)

提交回复
热议问题