Home > Software engineering >  ¿How can i to read request headers using django test?
¿How can i to read request headers using django test?

Time:01-25

I'm using ApiClient class but I don't know how to send a headers, I'm trying to do it like this, but the answer is that I'm not authorized.

I have been trying to solve it for several hours but I have not been able to, your contribution would be of great help.

self.client = APIClient()
self.jwt = response.json()["access"]
self.client.credentials(HTTP_AUTHORIZATION='Bearer '   self.jwt)
self.client.headers.update({'Origin': 'https://travelingapps.com.co'})

this is te response:

('status_code', 401), ('content_type', 'application/json'), ('content', 'UNAUTHORIZED')])

CodePudding user response:

you have 2 options.

  1. You can add the header you need when you instantiate APIClient(), just pass the header as a parameter. example: self.client = APIClient(HTTP_ORIGIN="https://travelingapps.com.co") In this way, all the requests you make with APIClient() will carry that header. remember that you must set a valid header.

2.If you want to send the header only in a request, you can add it to the post method you are doing.

response = self.client.post(
           self.url,
           {
            'email': "[email protected]",
            'password': "abcd.1234",
           }, 
           format='json',
           HTTP_ORIGIN="https://travelingapps.com.co"
           )

Try it!

  • Related