不支持的媒体类型

不支持的媒体类型

本文介绍了“应用程序/vnd.api+json"抛出“不支持的媒体类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rest Assured 为 POST 调用自动化 API,对于 Content-Type 和 ACCEPT 标头,我必须使用application/vnd.api+json".但是每次我使用application/vnd.api+json"时,我都会得到 415 状态代码.尽管使用 Postman 进行相同的 POST 调用工作正常.

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.

这是我的示例代码:

    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();

以下是收到的回复

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": "[email protected]",
                    "phone": "5551234567"
                }
            ],
            "tip_amount": "1.00"
        }
    }
}

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

我已尝试按照其他帖子/评论的建议将 Content-Type 更改为 application/json,但这对我的资源来说似乎不正确.

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.

目前,我使用的是 Rest Assured v4.3.0 和 json-path v4.3.0.此外,为了构建请求正文,我使用了 com.google.gson.JsonObject 库.

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.

推荐答案

在日志中,您可以看到charset=ISO-8859-1"正在发送,它是由 Rest Assured 自动添加的,.config() 禁用它并且不发送字符集

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

试试下面的

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();

这也需要静态导入

导入静态io.restassured.config.EncoderConfig.encoderConfig;

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

这篇关于“应用程序/vnd.api+json"抛出“不支持的媒体类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:20