How to use different content type in jemter. - jmeter

Scenario: I've two post requests in jmeter one is used for token generation which has content type "application/x-www-form-urlencoded" and second is used for xyz operation by using that token has content type application/json, how can i perform this in jmeter?

For 1st request just make sure to choose POST value from the "Method" dropdown and make sure that Use multipart/form-data box is not ticked
JMeter will automatically send application/x-www-form-urlencoded request header for POST
For 2nd request add a HTTP Header Manager as a child and configure it to send the required Content-Type header

Related

Capture Cookie from Response Header in Login and use it to all subsequent steps in Request Headers

I cannot modify anything on user.properties to add next line CookieManager.save.cookies=true because I run my scripts from a remote machine in which I don't have access to.
I am able to capture the Cookie using a Regular Expression Extractor from RESPONSE HEADER in Login and save it in a variable 'Cookie_cp'. I tried adding 'Cookie_cp' on HTTP Cookie Manager and also tried adding it on HTTP Header Manager, but JMeter sends the cookie only on REQUEST BODY, but not the REQUEST HEADER.
Another issue is that 'Cookie_cp' is being captured from second step's RESPONSE HEADER and applied to third step, while I need this cookie to be applied only from Login to all other steps.
Any help please?
JMeter sends the cookie(s) in the Cookie header, not in the request body, if you're using View Results Tree listener to inspect the request and it shows the cookie in the "Request Body" tab it's kind of misleading.
In fact JMeter does send the cookie(s) in the Cookie header, you can double check it using a sniffer tool like Wireshark
If you want to send the cookie (or any other header) with all the requests starting from second one you can:
Put these requests under a Simple Controller
Add HTTP Cookie Manager (or HTTP Header Manager) at the same level as these requests
Both Cookie and Header manager obey JMeter Scoping Rules so the header(s) will be added to all Simple Controller's children

Unable to click parameters tab in Jmeter 5.2.1

I am trying to pass an authorization header along with a request body to an HTTP request in Jmeter. When i pass a parameterized body, I am not able to click on the parameters tab to pass a header, and if I add the header first, the body data tab becomes non-clickable. Please help.
Parameters are being used for the URL Parameters or for the Query String.
Use HTTP Header Manager to pass an authorization header along with your request body in JMeter.
Just right click on your HTTP sampler and then add HTTP Header Manager from the config element.
In Header Manager, You can add multiple header information along with the request.

Post Request getting failed due to webkitformboundary in JMeter

We have a recorded script using Blaze Meter(HTTPS call) and run in JMeter, One POST request getting failed and shows "500 Internal Server error". In Post request, JSON passed in form of "form-Data" as parameter. When endpoint executed we observed Webkitformbounday in HTTP header, Please give some solution for
HTTPs Request
HTTP Header Manager
POST Request Body
enter image description here
It looks like the recording solution you're using isn't very suitable for building proper HTTP POST request, I would suggest the following amendments:
Untick Use multipart/form-data box in the HTTP Request sampler
Remove issueDetail parameter name and put it to the end of "path"
Change Content-Type header value to application/json
Authorization header value might need to be correlated if the token life span is short
More information: Testing SOAP/REST Web Services Using JMeter

Jmeter: How to override Content-Type in request to sent multipart/form-data

In all my requests I need Content-Type=application/json, so I added HTTP Header manager under the Test Plan.
But then inside of one of the threads, I need to upload image as a multipart request with additional parameters. I created HTTP request sampler, checked 'Use multipart/form-data', added file on File Uploads tab, added parametres on the Parameters tab. But Content-Type=application/json was sent in headers, so the request was failed.
I tried:
To add additional HTTP Header manager under the request with
Content-Type=multipart/form-data;
boundary=----WebKitFormBoundary6dcBYcLBBPisdP1e. But in the body of
the sent request, Jmeter added another boundary (randomly generated)
so the request was failed
To add Content-Type=multipart/form-data without boundary, but it didn't work (error that boundary is required)
To write body of multipart request myself, so my boundary is used. It worked for txt files, but I had problem with reading data from image file, so all images were broken when loaded (I used ${__FileToString(${pathToImage},,)} to read file content and add it the request body).
Is there any option to override content type as multipart/form-data and to add boundary that will be then used by Jmeter in body? I can remove the header manager from the test plan, in that case multipart request will be sent with Content-Type=multipart/form-data; boundary=----%randomly_generated_by_jmeter%. But in that case I'll need to add application/json content type to all other requests (and there are lot of them).

The reference value not send along with request headers in JMeter

I have an HTTP Request sampler in JMeter which sends a login request.
I have to obtain the access_token from the response of this HTTP request which has to be sent along with the request header of the 2nd HTTP request Sampler.
The response data of login request is something like this:
{"status":true,"access_token":"91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM"}
Regular Expression Extractor
Reference Name: access_token
Regular expression: access_token":"(.*?)"
Template $1$
MatchNo:1
When I am checking this regular expression using View Results Tree->RegExp Test, I am obtaining a result like this:
Match count: 1
Match[1][0]=
access_token":"91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM"
Match[1][1]=
91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM
What I need is to obtain Match[1][1]. ie just the access_token value.
Referring the value of the expression in my HTTP Header Manager of the 2nd HTTP request Sampler:
Name: Authorization
Value : Bearer ${access_token}
Now when I am running my JMeter test and observe the View Results Tree, I can see that the response of the second request is
Global Authentication Failed
and the Request Headers:
Connection: keep-alive
problem is in the following line: I have to get the access_token value (91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM) instead of ${access_token}:
Authorization: Bearer ${access_token}
Content-Type: application/json
Content-Length: 21
Host: localhost
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_111)
If instead of giving the reference token I am hard-coding the token in HTTP Header Manager then the response is successful.
Note: I have already checked [How to get Response Data and parse it to HTTP Header Manager in JMeter
How can I solve this issue?
Most likely your Regular Expression Extractor fails due to extra space or line break or whatever.
Be aware that since Apache JMeter 3.0 you have JSON Extractor which is designed for getting data from JSON response types using Json Path language. The relevant JSON Extractor configuration would be as simple as:
See API Testing With JMeter and the JSON Extractor article for more information on.
Apart from this, the remaining elements configuration in your Test Plan looks fine.

Resources