如何使用JMeter通过HTTP请求发送数据

huangapple go评论78阅读模式
英文:

how to use jmeter to send data using a http request

问题

I try to use Jmeter and a http request to send data to a controller using a function to create 5 new user with the same data.

Jmeter HttpRequest:
如何使用JMeter通过HTTP请求发送数据

the function inside the controlleur:

public Result create(Http.Request request){
    var body = safeJsonBody(request);
    var mandatory_params = Ref.of(List.of("username", "firstname", "email", "password", "phonenumber", "address"));
    var maybeUsername = extractNonEmptyString(body, "username");
    var maybeFirstname = extractNonEmptyString(body, "firstname");
    var maybeEmail = extractEmail(body, "email");
    var maybePassword = extractNonEmptyString(body, "password");
    var hashedPassword = "";
    if(!maybePassword.isEmpty())
    {
        var hash = hashFactory.mkHash(maybePassword.get(), Registration);
        hashedPassword= hash.get().hash;
    }
    var maybePhonenumber = extractNonEmptyString(body, "phonenumber");
    var maybeAddress = extractNonEmptyString(body, "address");

    logger.warn("maybeUsername                           : " + maybeUsername);
    logger.warn("maybeFirstname                           : " + maybeFirstname);
    logger.warn("maybeEmail                           : " + maybeEmail);
    logger.warn("maybePassword                           : " + maybePassword);
    logger.warn("maybePhonenumber                           : " + maybePhonenumber);
    logger.warn("maybeAddress                               : "+maybeAddress);

    if ( maybeUsername.isEmpty() || maybeFirstname.isEmpty() || maybeEmail.isEmpty() || maybePassword.isEmpty()) {
        checkIfIsPresent(mandatory_params,maybeUsername,"username");
        checkIfIsPresent(mandatory_params,maybeFirstname,"firstname");
        checkIfIsPresent(mandatory_params,maybeEmail,"email");
        checkIfIsPresent(mandatory_params,maybePassword,"password");
        checkIfIsPresent(mandatory_params,maybePhonenumber,"phonenumber");
        checkIfIsPresent(mandatory_params,maybeAddress,"address");

        return badRequest("Missing mandatory params: " + mandatory_params);
    }

    var tx = usersModel.upsert(maybeUsername.get(),maybeFirstname.get(),
            maybeEmail.get(),hashedPassword,maybePhonenumber.get(),maybeAddress.get());
    if (tx.isEmpty()){
        logger.warn(tx.toString());
        return internalServerError(stc_map_to_json(Map.of("status","error","message", "The user could not be created or updated.")).get());
    }
    return ok(tx.get());
}

I see "Missing mandatory params: Ref{val=[username, firstname, email, password, phonenumber, address]}" inside the response body in the View result tree I created so the data must be going to the right function.

And I try display the content of the request send to the controller by using a logger and it's weirdly display "POST /user/create"

英文:

I try to use Jmeter and a http request to send data to a controller using a function to create 5 new user with the same data.

Jmeter HttpRequest:
如何使用JMeter通过HTTP请求发送数据

the function inside the controlleur:

public Result create(Http.Request request){
var body = safeJsonBody(request);
var mandatory_params = Ref.of(List.of("username", "firstname", "email", "password", "phonenumber", "address"));
var maybeUsername = extractNonEmptyString(body, "username");
var maybeFirstname = extractNonEmptyString(body, "firstname");
var maybeEmail = extractEmail(body, "email");
var maybePassword = extractNonEmptyString(body, "password");
var hashedPassword = "";
if(!maybePassword.isEmpty())
{
var hash = hashFactory.mkHash(maybePassword.get(), Registration);
hashedPassword= hash.get().hash;
}
var maybePhonenumber = extractNonEmptyString(body, "phonenumber");
var maybeAddress = extractNonEmptyString(body, "address");
logger.warn("maybeUsername                           : " + maybeUsername);
logger.warn("maybeFirstname                           : " + maybeFirstname);
logger.warn("maybeEmail                           : " + maybeEmail);
logger.warn("maybePassword                           : " + maybePassword);
logger.warn("maybePhonenumber                           : " + maybePhonenumber);
logger.warn("maybeAddress                               : "+maybeAddress);
if ( maybeUsername.isEmpty() || maybeFirstname.isEmpty() || maybeEmail.isEmpty() || maybePassword.isEmpty()) {
checkIfIsPresent(mandatory_params,maybeUsername,"username");
checkIfIsPresent(mandatory_params,maybeFirstname,"firstname");
checkIfIsPresent(mandatory_params,maybeEmail,"email");
checkIfIsPresent(mandatory_params,maybePassword,"password");
checkIfIsPresent(mandatory_params,maybePhonenumber,"phonenumber");
checkIfIsPresent(mandatory_params,maybeAddress,"address");
return badRequest("Missing mandatory params: " + mandatory_params);
}
var tx = usersModel.upsert(maybeUsername.get(),maybeFirstname.get(),
maybeEmail.get(),hashedPassword,maybePhonenumber.get(),maybeAddress.get());
if (tx.isEmpty()){
logger.warn(tx.toString());
return internalServerError(stc_map_to_json(Map.of("status","error","message", "The user could not be created or updated.")).get());
}
return ok(tx.get());
}

I see "Missing mandatory params: Ref{val=[username, firstname, email, password, phonenumber, address]}" inside the response body in the View result tree I created so the data must be going to the right function.

And I try display the content of the request send to the controller by using a logger and it's weirdly display "POST /user/create"

答案1

得分: 1

{
"username": "userJM",
"firstname": "usernameJM",
"email": "userJM@gmail.com",
"password": "mdpJM",
"phonenumber": 12345678934,
"address": "150 rue jmeter"
}

在JMeter中,应该是类似这样的:

如何使用JMeter通过HTTP请求发送数据

你可能还需要添加一个HTTP Header Manager并配置它以发送Content-Type头部,值为application/json

如何使用JMeter通过HTTP请求发送数据

如果你不确定如何正确构建此请求,最简单的方法是使用JMeter的HTTP(S)测试脚本记录器进行录制

}

英文:

It looks like you need to send a JSON structure like:

{
"username": "userJM",
"firstname": "usernameJM",
"email": "userJM@gmail.com",
"password": "mdpJM",
"phonenumber": 12345678934,
"address": "150 rue jmeter"
}

In JMeter it would be something like:

如何使用JMeter通过HTTP请求发送数据

You might also need to add a HTTP Header Manager and configure it to send Content-Type header with the value of application/json

如何使用JMeter通过HTTP请求发送数据

If you're not sure how to properly build this or that request the easiest way is just recording it using JMeter's HTTP(S) Test Script Recorder

huangapple
  • 本文由 发表于 2023年2月23日 21:27:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定