Status.find.in(200), but actually found 401

Hi Team,
Can anyone tell me what is wrong in my code. When I pass the hardcoded bearer token (JWT) directly in fetchSetupData() API everything works fine but when I am trying to generate bearer token dynamically I am getting error 401

object expensesChain {
  val url = s"$baseUrl"+"/api/erp/expenses"
  val jsonHeader = Map("Content-Type" -> "application/vnd.oracle.adf.resourceitem+json")
  val authorization2 = Map("Authorization" -> "Basic REDACTED")
  val restFrameworkVersion =  Map("REST-Framework-Version" -> "6")

  private var token  = ""
  val auth = exec(getAuthToken())

  def getAuthToken() =
    exec(
      http("POST OAuth Req")
        .get("https://epiw.gamma.spectra.us-phoenix-1.ocs.oc-test.com/api/erp/tokenRelay")
        .headers(authorization2)
        .check(bodyString.saveAs("access"))
        .check(status is 200))
      .exec{session => { token = session("access").as[String]
        session}}

  val authorization3 = Map("authorization" -> "Bearer ${access}")

  def fetchSetupData () =
    exec(
      http("Expenses - fetchsetupdata")
        .post(url+"/resources/v0/expenses/action/fetchSetupData")
        .header("content-type","application/vnd.oracle.adf.action+json")
        .headers(authorization3)
        .headers(restFrameworkVersion)
        .body(ElFileBody("bodies/fetchSetupData.json")).asJson
        .check(status.in(200))
    )
}

Hi @harshit,

I don’t see any hardcoded bearer token. Only a hardcoded basic auth: “`Redacted” (base64 is not encryption, if this are real credentials, please change).

401 Unauthorized means that the client doesn’t provide credentials and server ask to retry (browser usually open a modal for basic auth)

In your case, you added a map of headers. Please note that you have a Map[String, String] not a Map[String, Expression[String]].
The value won’t be interpreted by Gatling engine to replace the access

You should use the singular header method that accept an Expression[String]

  .header(HttpHeaderNames.Authorization, "Bearer #{access}")

Does that help?
If not, please provide whole (but smallest) simulation.

Cheers!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.