How to resolve error 415 Unsupported Media Type when Content-Type is correctly set

岁酱吖の 提交于 2019-12-11 07:58:24

问题


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"
}

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

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.

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' }

Any help is appreciated.


回答1:


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:

How can I send just 'application/json' as the content-type header with Karate?

Posting this to help others in future.




回答2:


Try adding a * header Accept = 'application/json' header. The one difference between Karate and Postman is that Postman tries to be smart and auto-adds an Accept header - whereas Karate does not.



来源:https://stackoverflow.com/questions/52882944/how-to-resolve-error-415-unsupported-media-type-when-content-type-is-correctly-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!