Passing token to Locust request

Question:

I am trying to pass the Oauth token generated into a @task request

This is resulting in a 401 error

from locust import HttpUser, constant, task
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class ProcessRequests(HttpUser):

    def on_start(self):

        tenant_id = "tenant123"
        client_id = "client123"
        secret = "secret123"
        scope = "api://123/.default"

        body ="grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + secret + "&scope=" + scope

        tokenResponse = self.client.post(
                f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token",
                body,
                headers = { "ContentType": "application/x-www-form-urlencoded"} 
            )

        response = tokenResponse.json()
        responseToken = response['access_token']
        

        self.headers = {'Authorization': 'Bearer' + responseToken}


    @task
    def get_labware(self):
        self.client.get("https://url/123", name="Labware",headers=self.headers)

    @task
    def get_instruments(self):
        self.client.get("https://url/456", name="Instruments", headers=self.headers)

I got this to work in K6/javascript so I know the parameters are correct. I just must not be parsing or passing them correctly here in python.

Asked By: JD2775

||

Answers:

You need a space between Bearer and your token. Try this:

self.headers = {'Authorization': 'Bearer ' + responseToken}
Answered By: Solowalker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.