首页 > 解决方案 > Magento2 400 使用 Apache JMeter 获取产品的错误请求

问题描述

我正在尝试使用 JMeter 对 Magento 2 API 的结帐过程进行负载测试。我们预计有 1000 个并发用户。

以下 curl 命令可以正常工作:

curl -g -X GET "https:/oursite.com/rest/V1/products/25006A/" -H "Authorization: Bearer tm2yd2908exmy0ntoq9esvr5gqoh7upp"

但是,当我在 JMeter 的标头中将相同的 Authorization Bearer 放入相同的 URL 时,它不会返回 JSON。相反,它会抛出 400 Bad Request。我还需要哪些其他标题?

这是我的 JMeter 脚本:

<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="Get Products" enabled="true">
  <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
    <collectionProp name="Arguments.arguments"/>
  </elementProp>
  <stringProp name="HTTPSampler.domain">${varDomain}</stringProp>
  <stringProp name="HTTPSampler.port"></stringProp>
  <stringProp name="HTTPSampler.protocol">https</stringProp>
  <stringProp name="HTTPSampler.contentEncoding"></stringProp>
  <stringProp name="HTTPSampler.path">/rest/V1/products/25006A/</stringProp>
  <stringProp name="HTTPSampler.method">GET</stringProp>
  <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
  <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
  <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
  <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
  <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
  <stringProp name="HTTPSampler.implementation">HttpClient4</stringProp>
  <stringProp name="HTTPSampler.connect_timeout"></stringProp>
  <stringProp name="HTTPSampler.response_timeout"></stringProp>
</HTTPSamplerProxy>
<hashTree>
  <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
    <collectionProp name="HeaderManager.headers">
      <elementProp name="" elementType="Header">
        <stringProp name="Header.name">Authorization Bearer</stringProp>
        <stringProp name="Header.value">${varToken}</stringProp>
      </elementProp>
      <elementProp name="" elementType="Header">
        <stringProp name="Header.name">content-type</stringProp>
        <stringProp name="Header.value">application/json</stringProp>
      </elementProp>
    </collectionProp>
  </HeaderManager>
  <hashTree/>
</hashTree>

标签: jmetermagento2magento-2.3

解决方案


JMeter 5.1开始,可以从curl命令创建 JMeter 测试计划

  1. 从 JMeter 的主菜单中选择 Tools -> Import from cURL
  2. 使用以下命令:

    curl "https:/oursite.com/rest/V1/products/25006A/" -H "Authorization: Bearer tm2yd2908exmy0ntoq9esvr5gqoh7upp"
    
  3. 就是这样,JMeter 将生成具有正确值的HTTP 请求采样器和HTTP 标头管理器

    在此处输入图像描述


您的脚本的问题是标题名称应该是Authorization,标题值应该是Bearer tm2yd2908exmy0ntoq9esvr5gqoh7upp


推荐阅读