正确设置Content-Type时如何解决错误415不支持的媒体类型 [英] How to resolve error 415 Unsupported Media Type when Content-Type is correctly set
问题描述
我有一个简单的POST请求,它需要一个json Content-Type标头和一个类似
I have a simple POST request that requires a json Content-Type header and a body like
{
oneNbr: "2016004444",
twoCode: "@@@",
threeNbr: "STD PACK",
sheetTitle: "010000",
codeType: "AF14"
}
当我在Postman中运行它时,它按预期运行,并返回200状态和预期响应.
When I run this in Postman, it runs as expected, returning 200 status and the expected response.
这是空手道中的相同剧本:
Here's the same script in Karate:
Scenario: Sample test
* def payload =
"""
{
oneNbr: "2016004444",
twoCode: "@@@",
threeNbr: "STD PACK",
sheetTitle: "010000",
codeType: "AF14"
}
"""
Given path '/my_end_point/endpoint'
And request payload
When method post
Then status 200
运行此命令时,它将返回{"code":"415","status":不支持的媒体类型"}.控制台输出显示在POST期间设置了正确的内容类型.
When I run this, it returns {"code":"415","status":"Unsupported Media Type"}. The console output shows that the right content-type is being set during the POST.
即使我在脚本中专门设置了内容类型,仍会返回415,例如
Even if I specifically set the content-type in the script, 415 is still returned e.g.
And header Content-Type = 'application/json'
OR
* configure headers = { 'Content-Type': 'application/json' }
感谢您的帮助.
推荐答案
我们进行了一些调试,发现空手道自动将'charset = UTF-8'附加到Content-Type标头中. API不希望使用字符集.
We did some debugging and found that Karate automatically appends 'charset=UTF-8' to the Content-Type header. The API does not expect charset.
找到了以下帖子,并解决了该问题:
Found the following post and that solved the problem:
发布此信息以帮助将来的其他人.
Posting this to help others in future.
这篇关于正确设置Content-Type时如何解决错误415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!