如何使用 JMeter 将 application/x-www-form-urlencoded 参数发送到 RestServer?

问题描述 投票:0回答:6

我开发了一个休息服务器,并将其在本地主机中运行,我尝试使用 JMeter 执行测试,发送请求帖子和获取(取决于调用的方法)。

我已经发送到 Rest 服务器并使用 JMeter 在简单的 post 请求、获取请求、通过 post 发送文件以及通过 post 发送 Json 中获得结果。

但我不知道如何将

Form-UrlEncoded
对象发送到服务器。我的Rest服务器消耗
application/x-www-form-urlencoded
,我需要发送3个字符串参数。

有某种方法可以为每个参数设置

MimeType
并执行测试吗?

我使用的是Jmeter 2.7

  • [更新]

我通过禁用该选项解决了这个问题:

use multipart/form-data for post

并启用:

redirect automatically

代替:

follow redirect

我通常将参数放在“随请求发送参数”表中,并带有各自的名称。

rest mime-types jmeter form-data
6个回答
30
投票

要将表单参数作为 application/x-www-form-urlencoded 发送,请添加一个标头参数 Content-Type,其值为 application/x-www-form-urlencoded。

以下步骤适用于Jmeter 2.3.4

  1. 在您的 http 请求下添加 HTTP 标头管理器。
  2. 向 HTTP 标头管理器添加新参数,名称为 Content-Type,值为 application/x-www-form-urlencoded。
  3. 取消选中 HTTP 请求的“使用 multipart/form-data for HTTP POST”。
  4. 取消选中“编码?”每个请求参数(非必须)。
  5. 将 HTTP 请求的“内容编码:”文本框保留为空。

    这不适用于 PUT 请求。 对于 put 请求,添加参数作为路径参数并设置 Content-Type 标头,然后 Jmeter 将自行完成。


14
投票

这是使用 jmeter 进行 x-www-form-urlencoded 测试的 HTTP POST 解决方案。你只要跟着这些就行了。

进入线程组 -> 添加监听器 -> 在表中查看结果,查看结果树。查看响应过程。


2
投票

您是否尝试过使用 BadBoyJMeter Proxy 保存测试以查看应用程序实际发送的内容?

要查看幕后发生的情况,您还可以使用

FireBug
(如果您使用的是 FireFox)或
Ctrl+Shift+i
(如果您使用的是 Chrome)。


0
投票
仅当您在 HTTP 标头管理器中为 HTTP 请求添加所需的内容类型后,

IllegalCharsetNameException 才会立即发生。

希望这有帮助。


0
投票

步骤:

  1. 在 HTTP 请求的参数选项卡中添加表单数据(编码)
  2. HTTP 标头管理器中的
    Content-Type   application/x-www-form-urlencoded
    中的内容类型解决了我的问题

-3
投票

按照提到的确切步骤操作,我仍然看到抛出异常

响应代码:非 HTTP 响应代码:java.nio.charset.IllegalCharsetNameException 响应消息:非HTTP响应消息:application/x-www-form-urlencoded

java.nio.charset.IllegalCharsetNameException: application/x-www-form-urlencoded
    at java.nio.charset.Charset.checkName(Charset.java:315)
    at java.nio.charset.Charset.lookup2(Charset.java:484)
    at java.nio.charset.Charset.lookup(Charset.java:464)
    at java.nio.charset.Charset.forName(Charset.java:528)
    at org.apache.http.entity.ContentType.create(ContentType.java:210)
    at org.apache.http.entity.StringEntity.<init>(StringEntity.java:116)
    at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sendPostData(HTTPHC4Impl.java:1340)
    at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.handleMethod(HTTPHC4Impl.java:592)
    at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:409)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1166)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1155)
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:475)
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:418)
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:249)
    at java.lang.Thread.run(Thread.java:745)
© www.soinside.com 2019 - 2024. All rights reserved.