简体   繁体   中英

JSON in HTTP request to API using JMeter

I am trying to test a web API using JMeter. The web server interacts between JMeter and a database. The server is supposed to take parameters from the JSON in HTTP POST requests and parse them into queries.

The code of the web server in the relevent controller looks like this:

    [HttpPost]
    [Route("my_awesome_route_path")]
    public async Task<List<QueryResult>> invoice([FromBody]InvoiceInfo invoiceInfo) 
    {
     query = string.Format("INSERT INTO my_awesome_table (`my`,`awesome`,`columns`) " +
                       "VALUES ({0},{1},{2});", invoiceinfo.awesomeparam1, ... ,invoiceinfo.awesomeparamn);

          noRowsInserted = await db.Database.ExecuteSqlCommandAsync(query);

And In my JMeter HTTP request form I entered the correct path for the above controller function. I left the parameters and files upload tabs empty while filling the body data tab with the following:

${__FileToString(/my/awesome/file/path${__eval({awesome_json_file_name_with_no_dottxt_in_end_altough_it_has_it})}.txt,,)}

Running the server and with no body data does reach the function to a breakpoint I set. So I know the route path is fine. Adding the above code to the body data causes a 415 error - Unsupported Media Type. Here is the httpsample provided by an output file by JMeter:

<httpSample t="8023" it="0" lt="8023" ct="2" ts="1544434665189" s="false" lb="HTTP Request" rc="415" rm="Unsupported Media Type" tn="Thread Group 1-3" dt="text" by="382" sby="281" ng="1" na="1">
<responseHeader class="java.lang.String">HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: todays_awesome_date
Content-Length: 103
</responseHeader>
  <requestHeader class="java.lang.String">Connection: keep-alive
Content-Length: 37
Content-Type: multipart/form-data; boundary=jBtzRzXGjCNVf_3UH8LfxguE6qR9_xH
Host: awesome_ip_add
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_191)
</requestHeader>
  <responseData class="java.lang.String">{"Message":"The request entity's media type 'multipart/form-data' is not supported for this resource.'}</responseData>
  <cookies class="java.lang.String"></cookies>
  <method class="java.lang.String">POST</method>
  <queryString class="java.lang.String">--jBtzRzXGjCNVf_3UH8LfxguE6qR9_xH--&#xd;
</queryString>
  <java.net.URL>awesome_route_path</java.net.URL>
</httpSample>

The issue is also not with the sql syntax. It run fine from another function in the controller when parameters were given through the URL.

I also tried filling the files upload with the path and a paramter name and then in parameters to add the file and It always gives out the same error code. I just want the JSON parameters to fill the function's argument so it can be used as shown in code. What is it I'm missing? Thanks.

  1. Add HTTP Header Manager as a child of the HTTP Request sampler
  2. Configure it to send Content-Type header with the value of application/json

    在此处输入图片说明

  3. Make sure you have Use multipart/form-data for POST NOT CHECKED in the HTTP Request sampler

    在此处输入图片说明

More information: REST API Testing - How to Do it Right

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM