19

I have a jmeter login script where user logs in and logs out. The detailed screenshots are attached below.

Request data is as attached: The request data content

In the response date , the authorization token is generated:

enter image description here

And the regular expression for the same is as below: enter image description here

I am passing the value as parameter in 55/users:

enter image description here

When I'm running the script it is failing: enter image description here

Here is the response data:

enter image description here

ouflak
  • 2,458
  • 10
  • 44
  • 49
dhairya
  • 375
  • 1
  • 4
  • 10
  • the error message says that the server wants this token in a HEADER, not as a URL parameter. Here's the solution you need to use: http://stackoverflow.com/questions/24542747/jmeter-alter-http-headers-during-test – timbre timbre Sep 18 '16 at 17:51
  • i tried but still the script does not work – dhairya Sep 19 '16 at 08:30
  • sending bearer as a parameter of GET won't work either. Try to get that script working, or post question detailing what's not working in it – timbre timbre Sep 19 '16 at 14:37

7 Answers7

40

Use Header Manager to pass the Token as a Header so you would have:

Header Manager

See for more details:

https://stackoverflow.com/a/43283700/460802

If you're looking to learn jmeter correctly, this book will help you.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
7

A bit easier JMeter setup (login/get):

Thread Group

  • HTTP Request, Body Data: { "Login":"some", "Password":"credentials" }

    • HTTP Header Manager: content-type application/json
    • JSON Extractor - Names of created variables: Token; JSON Path expression: tokenName (root level in my case)
  • HTTP Request

    • HTTP Header Manager: content-type -> application/json; Authorization -> Bearer ${Token}
    • Response Assertion: Fields to Test = Response Code; Pattern Matching Rules = Equals, Not; Pattern to Test 401
  • View Results Tree to check results

Local IE Ajax version in case...

<SCRIPT>
var baseUri = 'https://localhost:port';
var tokenUri = '/something';
var getUri = '/restrictedData';
var token;
var form = { "Login":"some", "Password":"credentials" };
postRequest(baseUri + tokenUri, form, gotToken)

function gotToken(progress) {
    var response = progress.srcElement;
    if (response.status != 200) {
        document.body.innerText = "Error:\n" + response.response;
        return;
    }
    token = JSON.parse(response.response);
    console.log(JSON.stringify(token));
    var restricted = getRequest(baseUri + getUri, token.tokenName, gotRestricted);
}
function gotRestricted(progress) {
    var jsonStr = progress.srcElement.response;
    var jsonObj = JSON.parse(jsonStr);
    document.body.innerText = JSON.stringify(token,null,2) + '\n\n' + JSON.stringify(jsonObj,null,2);
}
function getRequest(url, token, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onloadend = callback;
    xhr.open('GET', url);
    xhr.setRequestHeader('contentType', 'application/json')
    if (token) xhr.setRequestHeader("Authorization", "Bearer " + token);
    xhr.send();
    return xhr;
}
function postRequest(url, body, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onloadend = callback;
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-Type', 'application/json')
    xhr.send(JSON.stringify(body));
    return xhr;
}
</SCRIPT>
Jan
  • 2,178
  • 3
  • 14
  • 26
2

Add Bearer ${token} in HTTP Header Manager available under failing HTTP Request.

image HTTP Request

4b0
  • 21,981
  • 30
  • 95
  • 142
2

If you already have the bearer token and just want to use in in header manager then, in HTTP HEADER MANAGER tab, put these values under NAME and VALUE column respectively.

Name: Authorization                        
Value: Bearer "add your actual token without quotes"
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Sharat M M
  • 21
  • 1
1

I got cUrl from my API and then I imported it. enter image description here

0

use Authorization as parameter name and value should be Bearer ${variable_name}

vinod chambole
  • 164
  • 1
  • 4
0

Once you've extracted the token from the token API request, use this token in the HTTP Authorization Header manager for subsequent API's. Example below:

Header Name: Header Value Authorization: Bearer ${generated_token}

Where "generated_token" is a variable containing the extracted token.

ouflak
  • 2,458
  • 10
  • 44
  • 49