JMeter: How to send request with content type header?

HttpJmeter

Http Problem Overview


How can we change content type in request header of http request for restful web services? From where do we exactly change it?

Http Solutions


Solution 1 - Http

Add HTTP Header Manager and add your desired header there:

Header Manager with Content-Type header

Solution 2 - Http

TL;DR To add Content type header you must have HTTP Header Manager associated to HTTP Request


You must add it for : >GET, DELETE, PUT and PATCH require a Content-Type.

Without overriding your request will be sent with defaults as:

Connection: keep-alive
Content-Length: [length of body]
Content-Type: text/plain
Host: [your host]
User-Agent: Apache-HttpClient/4.5.7 (Java/1.8.0_191)

Unlike using postman (advantage over JMeter), where you choose your content type in the same component, In JMeter you must have HTTP Header Manager which can be added as

  1. Child component of HTTP Request - effect only one request
  2. Same or above HTTP Request's hierarchy - may/will impact other HTTP requests in scope

Notice you can have multiple Header Managers:

>JMeter now supports multiple Header Managers. The header entries are merged to form the list for the sampler. If an entry to be merged matches an existing header name, it replaces the previous entry. This allows one to set up a default set of headers, and apply adjustments to particular samplers. Note that an empty value for a header does not remove an existing header, it just replace its value.

Notice you can't use it in multipart/form-data: >When using multipart/form-data, this suppresses the Content-Type and Content-Transfer-Encoding headers; only the Content-Disposition header is sent.

It won't be sent it in redirected urls:

>Headers are sent for the initial request, and won't be sent for the redirect. This is generally only a problem for manually created test plans, as a test plan created using a recorder would continue from the redirected URL.

Solution 3 - Http

Steps to add HTTP Headers

  • Create an HTTP Request

Thread Group -> HTTP Request

  • Right-click on the HTTP Request and add config element "HTTP Header Manager"

HTTP Request -> Add -> Config Element -> HTTP Cache Manager

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsudeep guptaView Question on Stackoverflow
Solution 1 - HttpAndrei BotalovView Answer on Stackoverflow
Solution 2 - Httpuser7294900View Answer on Stackoverflow
Solution 3 - HttpRajeshView Answer on Stackoverflow