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

使用Karate发送含Multipart文件和字段的POST请求时遇404错误求助

Karate API多部分表单上传404错误排查

问题描述

用户尝试用Karate实现Form Data多部分文件及字段上传,编写的请求代码如下:

#configuring headers
Background: pre-condition
  * def functions = call read('classpath:supportingFiles/header.js')
  * configure headers = functions.getDocumentAPIHeaders()
#hitting the request
Scenario: upload file
  Given url _url
  * path 's3/upload?='
  * def fileText = read('orderValuationFileUpload.pdf')
  And multipart file document = { read:  'orderValuationFileUpload.pdf' }
  * multipart field s3Name = _Name
  * multipart field s3Key = _key
  And header Content-Type = 'multipart/form-data'
  When method post
  Then status 200

执行后返回404错误,响应信息:

Response:
Mixed: content-disposition: form-data; name="document"; filename="orderValuationFileUpload.pdf"
content-type: application/pdf; charset=UTF-8
content-length: 31199
Completed: true
IsInMemory: false
RealFile: C:\Users\admzn74\Workspace\benbroker_apitest\target\test-classes\b2bApplicationApi\appSubmission_F1\orderValuationFileUpload.pdf DefaultDeleteAfter: true

content-disposition: form-data; name="s3BucketName"; filename=""
content-type: text/plain; charset=UTF-8
content-length: 57
Completed: true
IsInMemory: true

content-disposition: form-data; name="s3Key"; filename=""
content-type: text/plain; charset=UTF-8
content-length: 74
Completed: true
IsInMemory: true


04:03:10.592 response time in milliseconds: 368
1 < 404
1 < Content-Type: multipart/form-data
1 < Access-Control-Allow-Origin: *
1 < Access-Control-Expose-Headers: APIm-Debug-Trans-Id, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-Global-Transaction-ID
1 < X-Backside-Transport: FAIL FAIL
1 < Connection: close
{ "httpCode":"404", "httpMessage":"Not Found", "moreInformation":"No resources match requested URI" }

排查解决步骤

  • 修正URI路径:代码中* path 's3/upload?='多了无效的?=后缀,这会导致请求URI与后端接口不匹配,直接改为* path 's3/upload'。
  • 移除手动设置的Content-Type头:Karate会自动为多部分请求生成包含正确boundary的multipart/form-data头,手动设置反而可能破坏请求格式,删除And header Content-Type = 'multipart/form-data'这一行。
  • 验证变量赋值:确认_url、_Name、_key这些变量已正确赋值,可通过print语句输出验证,比如* print _url。
  • 确认文件路径:虽然响应显示文件已找到,但确保orderValuationFileUpload.pdf的classpath路径正确,必要时使用完整路径如read('classpath:b2bApplicationApi/appSubmission_F1/orderValuationFileUpload.pdf')。

修正后的代码示例

#configuring headers
Background: pre-condition
  * def functions = call read('classpath:supportingFiles/header.js')
  * configure headers = functions.getDocumentAPIHeaders()

#hitting the request
Scenario: upload file
  Given url _url
  * path 's3/upload'
  * multipart file document = { read: 'orderValuationFileUpload.pdf' }
  * multipart field s3Name = _Name
  * multipart field s3Key = _key
  When method post
  Then status 200

内容的提问来源于stack exchange,提问作者Nandini CR

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 08:09:24