使用JMETER向具有二进制主体的RESTful API发出POST请求 [英] Making POST request to a RESTful API with binary body using JMETER

查看:282
本文介绍了使用JMETER向具有二进制主体的RESTful API发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RESTful API测试服务,这些方法接收带有二进制数据的POST请求.我使用JMeter进行测试.但是我很难用此工具发出这样的请求.

I test a service with RESTful API, the methods receive POST requests with binary data. I use JMeter for testing. But I have troubles in making such requests with this tool.

我使用BeanShell Sampler制作二进制数据并将其放入变量

I use BeanShell Sampler to make binary data and put it into a variable

import java.util.Base64;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.codec.binary.Base64;

String base64Str="Some string";
//Encode the string
byte[] bytes = Base64.encodeBase64(base64Str.getBytes());
vars.putObject("bytes", bytes);

然后是HTTP请求,在主体数据中放置了 $ {bytes} ,以使其从变量中读取原始字节并在POST请求中发送.但是然后我得到一个异常"java.lang.ClassCastException:[B不能转换为java.lang.String",这意味着在主体数据中,给定的变量 bytes 被转换为字符串.相当相似的问题是使用JMeter发送具有二进制主体的HTTP请求,但在注释中也有关于字符串转换的相同结论.也许可以通过发送请求文件来完成?

Then HTTP Request, where in Body Data I put ${bytes} in order to make it read the raw bytes from the variable and send it in a POST request. But then I get an exception "java.lang.ClassCastException: [B cannot be cast to java.lang.String", which means that in Body Data the given variable bytes is cast to string. The rather similar question is Sending HTTP requests with binary body using JMeter but there was done the same conclusion about string casting in the comments. Maybe it can be done with sending a file with the request?

有一个插件HTTP Raw Request,但是我找不到将请求发送到具体API方法的方法,而该插件只有port字段(也许在port后面写了一个方法路径?),我找不到一个将字节传递给它的方式.

There's a plugin HTTP Raw Request, but I cannot find a way to send request to a concrete API method, while the plugin has only field for port (maybe write a method path right after port?), and I cannot find a way to pass bytes to it.

推荐答案

我设法使用HTTP Request发出了一个带有二进制主体数据的POST请求.为了做到这一点,我在使用请求发送文件"中添加了一个条目,并仅填充了文件路径字段(不带参数名称和MIME类型).JMeter读取给定的文件,在这种情况下,将其字节发送为POST请求的正文.

I managed to make a POST request with binary body data using HTTP Request. In order to do it I added an entry to "Send files with the request" and filled only file path field (without parameter name and MIME type). JMeter reads the given file and in this case sends its bytes as in the body of a POST request.

在这里找到答案https://qnalist.com/questions/698175/does-jmeter-support-sending-arbitrary-binary-data-in-the-body-of-a-post 并导致了手册 http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Request

The answer was found here https://qnalist.com/questions/698175/does-jmeter-support-sending-arbitrary-binary-data-in-the-body-of-a-post and led to manual http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Request

这篇关于使用JMETER向具有二进制主体的RESTful API发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆