setting content type in rest assured

后端 未结 8 1201
广开言路
广开言路 2021-01-07 21:53

I\'m trying to invoke a rest call using rest assured. My API accepts, \"application/json\" as content type and I need to set in the call. I set the content type

相关标签:
8条回答
  • 2021-01-07 22:48

    I was facing something similar and after some time we noticed the problem was actually coming from the server side. Please check your call on Postman and see if when it's triggered you need to change it from HTML to JSON. If you need to do that, the backend may need to force the response to be in JSON format by adding its content type. Even if it's encoded in JSON you're still may need to do that.

    Thats the line of code we added:

    header('Content-type:application/json;charset=utf-8');
    

    .

      public function renderError($err){
       header('Content-type:application/json;charset=utf-8');
       echo json_encode(array(
           'success' => false,
           'err' => $err
       ));
    }
    

    And that's what was happening on the backend:

    Hope that can help somehow. :)

    0 讨论(0)
  • 2021-01-07 22:49

    I faced similar issue while working with rest-assured 2.7 version. I tried setting both the contentType and also accept to application/json but it didn't work. Adding carriage feed and new line characters at the end as the following worked for me.

    RestAssured.given().contentType("application/json\r\n")
    

    The API seems to be missing to add new line characters after Content-Type header due to which the server is not able to differentiate between the media type and the rest of the request content and hence throwing the error 415 - "Unsupported media type".

    0 讨论(0)
提交回复
热议问题