当前位置:网站首页>JMeter interface automation - how to solve the content type conflict of request headers
JMeter interface automation - how to solve the content type conflict of request headers
2022-07-27 19:03:00 【Test kid】
One 、 Preface
Usually we use Jmeter When doing interface automation , Add... To the thread group HTTP Header Manager , Used to manage public request header information . Ordinary interface automation is no problem , But for some special operation processes , Such as : First upload the file interface ( Header usage Content-Type: multipart/form-data), Then the operation is transmitted json Format interface ( Header usage Content-Type: application/json), The request header information of these two interfaces will conflict .
Next, we will solve this problem step by step according to the example .
Two 、 Original script
For example: 5 Interface , Together, it is an automated operation process .
1- Image upload
2- Get service data
3- Set the service picture
4- Set service status
5- Get service status
among :
“1- Image upload ” The interface request header is Content-Type: multipart/form-data
“3- Set the service picture ”、“4- Set service status ” The interface request header is Content-Type: application/json
Add... Under thread group HTTP Header Manager , Add value as Content-Type: application/json

“1- Image upload ” The interface is set Content-Type: multipart/form-data

Each interface adds a response assertion .

The script structure :

Execute the script :
“1- Image upload ” The request header of the interface is Content-Type: application/json, instead of Content-Type: multipart/form-data
therefore “1- Image upload ” Interface , Do not use public HTTP Request header information in header manager .

3、 ... and 、 Original script optimization 1
Optimize the original script
Will be public HTTP Adjust the position of the header manager , Not as a public request header , Instead, specify adding... To the interface that needs the request header .
“1- Image upload ” The interface is of upload type , Has been set in the request Content-Type: multipart/form-data
“2- Get service data ”、“5- Get service status ” The interface does not need to add a request header .
“3- Set the service picture ”、“4- Set service status ” The interface needs to add a request header (Content-Type: application/json), So add HTTP Header manager .

The script structure :

Execute the script :
All interfaces are successfully implemented .

“1- Image upload ” Interface request header information .

“3- Set the service picture ” Interface request header information .

shortcoming : When there are many interfaces , If a large number of interfaces need to be specified HTTP Header Manager , Maintenance is a little troublesome 、 And the script looks a little bloated , It is inconvenient to modify uniformly .
Four 、 Original script optimization 2
Optimize the original script
take “1- Image upload ” The interface is extracted from the original thread group , Put it separately into the new thread group , That is, two thread groups .“1- Image upload ” The interface is in a thread group , Other interfaces are in a thread group .
In the thread group where other interfaces are located HTTP Header Manager , Request header value unchanged .

The script structure :

Execute the script :
Run two thread groups at the same time , All interfaces are successfully implemented .


shortcoming : Although the automated script composed of two thread groups can run through the business process , But this method divides threads , It doesn't look like a whole . Especially when there are more and more thread group scripts , It's a little inconvenient to implement .
5、 ... and 、 The original script is finally optimized
Optimize the original script
because “1- Image upload ” The interface does not need the request header information in the public header , So add the BeanShell PreProcessor Preprocessor , Delete the information in the public request header .
Script code :
import org.apache.jmeter.protocol.http.control.HeaderManager;
// Get request header information
HeaderManager headers = sampler.getHeaderManager();
// Print all request header information
log.info(" Before deleting " + headers.getHeaders().getStringValue());
// Delete the information specified in the request header
sampler.getHeaderManager().removeHeaderNamed("Content-Type");
// Print all request header information
log.info(" After deleting " + headers.getHeaders().getStringValue());
At this time, the specified information in the public request header is deleted , Later interfaces will use this information , So add it back on the subsequent interface , for example “3- Set the service picture ” Interfaces will use public headers , Add BeanShell PreProcessor Preprocessor , Add the request header information to the public request header .
Script code :
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
// Get request header information
HeaderManager headers = sampler.getHeaderManager();
// Print all request header information
log.info(" Before adding " + headers.getHeaders().getStringValue());
// new One Header object
myHeader = new Header("Content-Type", "application/json");
// add to Header To the request header Manager
headers.add(myHeader);
// Print all request header information
log.info(" After adding " + headers.getHeaders().getStringValue());
After the request header information is successfully added , If this information is needed for subsequent interfaces , Then there is no need to add (BeanShell PreProcessor Preprocessor ), You can use it .
Public HTTP Header Manager , Request header value unchanged .

The script structure :

Execute the script :
All interfaces are successfully implemented .

“1- Image upload ” Interface request header information .

“3- Set the service picture ” Interface request header information .

View the log printed during execution , Print out the deletion and addition request header information .

Be careful : Public HTTP Header manager disable or delete , Executing the script will report an error .

Even if the script adds request header information , But the specified request header cannot be found .

边栏推荐
- WPS turns off annoying advertisements
- Personal Center - order business process
- 怎样产生标准分布或高斯分布的随机数
- ridis命令笔记
- JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)
- Docker - docker installation, MySQL installation on docker, and project deployment on docker
- Leetcode first day of question brushing
- C interface knowledge collection suggestions collection
- Imitation thread deduction
- Hash、Set、List、Zset、BitMap、Scan
猜你喜欢
随机推荐
Idea 2020.1 Community Edition download experience
【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
log4j. Properties log details
Kinect2 for Unity3D——AvatarDemo学习
Hash、Set、List、Zset、BitMap、Scan
Acquisition data transmission mode and online monitoring system of vibrating wire wireless acquisition instrument for engineering instruments
收下这份实操案例,还怕不会用Jmeter接口测试工具
Here are all the MySQL interview questions you can't expect (the latest version of 2022)
LeetCode 刷题 第二天
Low noise anion fan touch IC
瑞吉外卖笔记
Imitation thread deduction
Typeerror: conv2d(): argument 'padding' (position 5) must be multiple of ints, not STR [error]
专项测试之「 性能测试」总结
Was not registered for synchronization because synchronization is not active[resolved]
【微信小程序】项目实战—抽签应用
文件的上传和下载
Functions in JS and the use of DOM to obtain elements and event attributes
MySQL 05 存储过程
Aircraft collision detection









