【发布时间】:2021-05-24 08:26:46
【问题描述】:
我有一个带有 HTTP POST 端点的本地 Web 应用程序。 此端点接收从 SendGrid 发送的电子邮件。
我已将上一封电子邮件的正文保存为 requestBody.bin。
我正在尝试使用 JMeter 复制此 HTTP POST 请求。
我有这个 Beanshell 预处理器脚本:
FileInputStream in = new FileInputStream("C:\\Projects\\centaurjmeter\\src\\InboundEmails\\requestBody.bin");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[999024]; //Value of Content-Length from when I sent the actual email. So this should be more than enough just for the HTTP POST body.
for (int i; (i = in.read(buffer)) != -1; ) {
bos.write(buffer, 0, i);
}
in.close();
byte[] requestBody = bos.toByteArray();
bos.close();
vars.put("requestBody", new String(requestBody, "ISO-8859-1"));
我在 HTTP POST 请求的正文中使用 ${requestBody}:
这就是我的 JMeter 测试的样子:
在我的 HTTP POST 操作方法中,我将电子邮件的附件保存到文件中。
pdf 文件似乎略有损坏,每页仅显示部分文本。 .png 文件甚至无法打开。
我尝试过使用:
vars.put("requestBody", new String(requestBody));
vars.put("requestBody", new String(requestBody, "Windows-1252")); //ANSI
vars.put("requestBody", new String(requestBody, "ISO-8859-1")); //default encoding
但它们都不起作用。 “ISO-8859-1”导致的损坏最少,因为至少有一些文本出现在 pdf 中(但 .png 文件根本不起作用)。
我不能使用 HTTP 原始请求采样器,因为它不适用于 https。
如何从 requestBody.bin 获取字节并将它们正确发送到我的 HTTP POST 正文中?
更新
我阅读了https://stackoverflow.com/a/41892324/2063755 并尝试在“文件上传”选项卡中将 requestBody.bin 作为文件发送,但我得到的结果与使用 Beanshell 预处理器脚本相同。
更新
上面的链接实际上确实帮助我解决了这个问题。
我只需要添加这个额外的TE 标题:
【问题讨论】:
标签: http encoding jmeter beanshell jmeter-5.0