首页 > 解决方案 > 如何在放心的拦截器中设置内容类型?

问题描述

我已经在拦截器中设置了内容类型,但是在运行 API 时,没有调用拦截器。

package com.gdn.qa.module.athens.api.interceptors;

import com.gdn.qa.automation.core.restassured.ServiceInterceptor;
import io.restassured.specification.RequestSpecification;

import static io.restassured.RestAssured.given;

public class AthensIAGInterceptor implements ServiceInterceptor {
    @Override
    public boolean isSupport(String serviceName) {
        if (serviceName.equalsIgnoreCase("athens_iag")) {
            return true;
        }
        return false;
    }
    enter code here
    @Override
    public void prepare(RequestSpecification resp1) {
         resp1 = given().log().all().contentType("application/json");
    }
}

如果我缺少任何步骤,请帮助我。

标签: javarest-assured

解决方案


下面的示例是在发送请求之前设置 contentType(ContentType.JSON),同样在响应中,您可以通过将其放在 then() 之后来验证它是否相同。

    given().contentType(ContentType.JSON).queryParam("page","2").when().get("https://reqres.in/api/users").then().contentType(ContentType.JSON).assertThat().statusCode(200).log().all();

推荐阅读