1

I am automating an API for a POST call using Rest Assured and for Content-Type and ACCEPT header I have to use "application/vnd.api+json". But every time I use "application/vnd.api+json" I get 415 status code. Although the same POST call using Postman works perfectly fine.

Here is my sample code :

    ApiUtils.setBaseURI("xxxxx"); 
            ApiUtils.setBasePath("/orders"); 
            RequestSpecification request = RestAssured.given().auth().oauth2(BaseClass.token);
            request.header("ACCEPT", "application/vnd.api+json");
            request.header("Content-Type", "application/vnd.api+json");
            request.body(JsonCreator.createJson());
            Response response = request.post();

Below is the response received

Request method: POST
Request URI:    https://xxxxxx/orders

Headers:        ACCEPT=application/vnd.api+json
                Content-Type=application/vnd.api+json; charset=ISO-8859-1
Cookies:        <none>
Multiparts:     <none>
Body:
{
    "data": {
        "type": "orders",
        "attributes": {
            "external_id": "2020-04-04-172",
            "order_items": [
                {
                    "menu_item_id": "5d29ae25805aaf0009095410",
                    "variation_id": "5d29ae25805aaf0009095418",
                    "quantity": 1,
                    "note": "some note"
                }
            ],
            "revenue_center_id": "5d7b44021a2976000938da62",
            "order_type_id": "5d27329790a5ba0009386a75",
            "guests": [
                {
                    "first_name": "xx",
                    "last_name": "xx",
                    "email": "xx@gp.com",
                    "phone": "5551234567"
                }
            ],
            "tip_amount": "1.00"
        }
    }
}

{"errors":[{"status":415,"code":415,"title":"Content-Type must be JSON API-compliant"}],"jsonapi":{"version":"1.0"}}

I have tried changing the Content-Type to application/json as suggested by other posts/comment but that seems to be incorrect for my resource.

Currently, I am using Rest Assured v4.3.0 and json-path v4.3.0. Also, to frame the request body I am using com.google.gson.JsonObject library.

RISHI KHANNA
  • 354
  • 5
  • 23

1 Answers1

3

In the logs you can see "charset=ISO-8859-1" being sent which is added automatically by Rest Assured, the .config() disables that and the charset is not sent

Try the below

ApiUtils.setBaseURI("orders");
ApiUtils.setBasePath("/orders");
RequestSpecification request = RestAssured.given().auth().oauth2(BaseClass.token).header("Content-Type", "application/vnd.api+json").header("Accept", "application/vnd.api+json").config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).log().all();
request.body(JsonCreator.createJson());
Response response = request.post();

This also needs a static import

import static io.restassured.config.EncoderConfig.encoderConfig;

https://github.com/rest-assured/rest-assured/wiki/Usage#avoid-adding-the-charset-to-content-type-header-automatically

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • Same error: Status Code- 406 {"errors":[{"status":406,"code":406,"title":"Accept header must be JSON API-compliant"}],"jsonapi":{"version":"1.0"}} – RISHI KHANNA Apr 04 '20 at 18:05
  • Tried Code:---- ApiUtils.setBaseURI("orders"); ApiUtils.setBasePath("/orders"); RequestSpecification request = RestAssured.given().header("Content-Type","application/vnd.api+json") .config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))) .log().all(); request.body(JsonCreator.createJson()); Response response = request.post(); – RISHI KHANNA Apr 04 '20 at 18:06
  • I seems to be a different error and not the same error, you were getting 415 earlier and now I see 406. Also, in the above why do I not see the oauth and accept header being sent ? Nevertheless - I have updated my answer by including the oauth and accept header in the same format as that you have put up in the question, try this and let me know – Wilfred Clement Apr 05 '20 at 01:18
  • The only change in the original request of yours should be the .config method along with the static import and the remaining should be as such – Wilfred Clement Apr 05 '20 at 01:20
  • Yes Indeed. I missed to add headers which caused this error... your suggested changes worked well...can you please help me understand what was causing the initial error? – RISHI KHANNA Apr 05 '20 at 14:34
  • 1
    I have updated by answer with an explanation, please check now – Wilfred Clement Apr 05 '20 at 15:51