如何发送只有一个 json 字段的多部分请求?

How to send a multipart request with only one json field?

我正在尝试发送包含一个字段的多部分请求,但我在日志中找不到它。我的密码是

Feature: test

Scenario: test send

Given url 'https://apitester.com/api/send'
And multipart field myJson = {test:'send'}
When method post
Then status 200

但在日志中我只找到

1 > POST https://apitester.com/api/send
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 226
1 > Content-Type: multipart/mixed; boundary=57Gy9v3RTbI82kmdNf-nsWld3wK1rz6W99F
1 > Host: apitester.com
1 > User-Agent: Apache-HttpClient/4.5.12 (Java/14.0.1)

在 Postman 中我得到了这样的东西并且工作正常:

POST /api/send HTTP/1.1
Host: apitester.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="myJson"

{"test":"send"}
----WebKitFormBoundary7MA4YWxkTrZu0gW

如果我添加 And header Content-Type = 'application/json' 它会变得有点相似,但它也不起作用:

1 > POST https://apitester.com/api/send
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 218
1 > Content-Type: application/json; boundary=bzLjH96ptD4G7HSRWI65XMnIhllxtiO
1 > Host: apitester.com
1 > User-Agent: Apache-HttpClient/4.5.12 (Java/14.0.1)
--bzLjH96ptD4G7HSRWI65XMnIhllxtiO
Content-Disposition: form-data; name="myJson"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{"test":"send"}
--bzLjH96ptD4G7HSRWI65XMnIhllxtiO--

我该如何解决这个问题?

是的,如果 Content-Type 不是“可打印的”(例如二进制文件),空手道不会记录它。

关于故障排除,这可能很困难,但如果您与 server-side 的同事一起工作,您应该能够更快地解决问题。如果您 post 一个有效的 cURL 命令,这里的社区可以帮助您。

我们将在以后的版本中改进日志记录。