You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止Karate对请求参数中的特殊字符进行编码?

Karate测试中阻止参数特殊字符(分号)自动编码

在Karate测试中,当请求参数包含分号(;)时,使用param关键字设置参数会导致分号被自动编码为%3B。以下是测试代码及抓包结果:

测试代码

Feature: Executor for Test Cases with no request body
  Background:
    * configure ssl = false
    * configure charset = null
  Scenario Outline:
    * def destinationUrl = 'http://localhost:' + destinationPort
    * def reqHeaders = read(reqHeadersFile)
    * def requestPath = reqPath
#    * def requestPath = reqPath+'?'+reqParams
    * configure httpVersion = <httpVersion>
    #####################
    # Setup and send the request
    #####################
    Given url destinationUrl
    Given path requestPath
    And param zzz = '123;456'
    And param aaa = '123'
    Given headers reqHeaders
    When method reqMethod

抓包结果

GET /echo1?zzz=123%3B456&aaa=123 HTTP/1.1
sba-router-destination-service-name: localService
sba-router-destination-service-namespace: local
User-Agent: Apache-HttpAsyncClient/5.0-beta2 (Java/1.8.0_362)
Host: localhost:9876
Connection: keep-alive

HTTP/1.1 200 OK
content-type: application/json
echo-server-header: Openet
3gpp-Sbi-Message-Priority: 24
content-length: 478

{"serverHostname":"localService","reqeustMethod":"GET","requestHeaders":{"x-http2-scheme":["http"],"host":["172.22.0.2:8090"],"user-agent":["Vert.x-WebClient/4.4.4","Apache-HttpAsyncClient/5.0-beta2 (Java/1.8.0_362)"],"x-request-id":["2682e648-4002-747b-ad4b-d738c9bc536d"],"x-http2-stream-id":["3"],"x-http2-stream-weight":["16"],"content-length":["0"]},"requestUri":"/echo1?zzz=123%3B456&aaa=123","requestParams":{"zzz":["123;456"],"aaa":["123"]},"requestBody":"BODY NOT SET"}

解决方案

Karate的param关键字默认会对URL参数进行编码,要阻止分号被编码,可通过以下两种方式绕过自动编码逻辑:

1. 手动拼接参数到路径中

直接将参数拼接在请求路径里,不使用param关键字,这样分号就不会被编码。修改代码如下:

Feature: Executor for Test Cases with no request body
  Background:
    * configure ssl = false
    * configure charset = null
  Scenario Outline:
    * def destinationUrl = 'http://localhost:' + destinationPort
    * def reqHeaders = read(reqHeadersFile)
    # 手动拼接路径和参数
    * def requestPath = reqPath + '?zzz=123;456&aaa=123'
    * configure httpVersion = <httpVersion>
    #####################
    # Setup and send the request
    #####################
    Given url destinationUrl
    Given path requestPath
    Given headers reqHeaders
    When method reqMethod

如果参数值是动态的,可通过字符串拼接实现:

* def zzzValue = '123;456'
* def aaaValue = '123'
* def requestPath = reqPath + '?zzz=' + zzzValue + '&aaa=' + aaaValue

2. 直接构造完整请求URL

将域名、路径和参数直接拼接成完整URL,替换url和path的设置:

Feature: Executor for Test Cases with no request body
  Background:
    * configure ssl = false
    * configure charset = null
  Scenario Outline:
    * def reqHeaders = read(reqHeadersFile)
    # 构造完整带参数的URL
    * def fullUrl = 'http://localhost:' + destinationPort + reqPath + '?zzz=123;456&aaa=123'
    * configure httpVersion = <httpVersion>
    #####################
    # Setup and send the request
    #####################
    Given url fullUrl
    Given headers reqHeaders
    When method reqMethod

这两种方式都能避免Karate自动编码参数中的分号,确保请求URL中的分号保持原始形式。

内容的提问来源于stack exchange,提问作者Starbucks Admin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 08:14:52