Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...