Jmeter 使用 multipart/form-data

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

Jmeter with multipart/form-data

问题

在Jmeter中,我想要使用JSR223预处理器Groovy来准备multipart/form-data。

我尝试使用以下方式:

import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.content.StringBody
import org.apache.http.util.EntityUtils

def filePath = "path/to/file"
def file = new File(filePath)

def payload = "some payload"

def builder = MultipartEntityBuilder.create().setCharset(java.nio.charset.StandardCharsets.UTF_8)

builder.addPart("payload", new StringBody(payload, ContentType.TEXT_PLAIN))
builder.addPart("file", new FileBody(file, ContentType.APPLICATION_OCTET_STREAM, file.name))

def formData = builder.build()

vars.put("formData", formData)

但是出现了错误:"org.apache.jmeter.threads.JMeterVariables.put()的方法签名不适用于参数类型:(String, org.apache.http.entity.mime.MultipartFormEntity)"

如果我想要在脚本中准备multipart/form-data,然后在HTTP请求中使用它,我应该怎么做?

英文:

In Jmeter i wanted to prepare multipart/form-data by using JSR223 PreProcessor groovy

i try using
vars.put("formData", formData) like this

import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.content.StringBody
import org.apache.http.util.EntityUtils

def filePath = "path/to/file"
def file = new File(filePath)

def payload = "some payload"

def builder = MultipartEntityBuilder.create().setCharset(java.nio.charset.StandardCharsets.UTF_8)

builder.addPart("payload", new StringBody(payload, ContentType.TEXT_PLAIN))
builder.addPart("file", new FileBody(file, ContentType.APPLICATION_OCTET_STREAM, file.name))

def formData = builder.build()

vars.put("formData", formData)

and got error "No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (String, org.apache.http.entity.mime.MultipartFormEntity)"
what should i do if i want to prepare multipart/form-data in script and then using it in HTTP Request?

答案1

得分: 0

  1. vars 代表 JMeterVariables 类的实例。 vars.put() 函数假定第二个参数是一个 String,而您似乎试图传递其他内容。如果这是您想要的,请改用 vars.putObject()。或者您需要先将实体转换为字符串。

  2. 我认为您根本不需要在 Groovy 中这样做,您可以使用正常的 JMeter 的 HTTP 请求 取样器来执行多部分请求,您可以将 "payload" 放入请求参数中,将文件放入 "文件上传" 中,并勾选 使用 multipart/form-data 复选框:

    Jmeter 使用 multipart/form-data

    也可以使用 sampler 快捷方式来以编程方式执行,使用 HTTPSamplerProxy 类。

查看 Top 8 JMeter Java Classes You Should Be Using with Groovy 文章,获取有关上述内容以及其他可用于 JSR223 测试元素的 JMeter API 简写的更多信息。

英文:
  1. vars stands for JMeterVariables class instance. vars.put() function assumes that the 2nd argument is a String and you're trying to pass something else. If this is what you want you should use vars.putObject() instead. Or you need to convert the entity into a String first somehow

  2. I don't think you need to do this in Groovy at all, you can perform the multipart request using normal JMeter's HTTP Request sampler, you can put the "payload" into request parameters and the file to "Files Upload" and tick Use multipart/form-data box:

    Jmeter 使用 multipart/form-data

    It also can be done programmatically using sampler shortcut for the HTTPSamplerProxy class

See Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on the above and other JMeter API shorthands available for the JSR223 test elements

huangapple
  • 本文由 发表于 2023年6月12日 12:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453614.html
匿名

发表评论

匿名网友

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

确定