简体   繁体   English

服务无法识别RestAssured内容类型application / json

[英]RestAssured content-type application/json not being recognised by service

I have a service, which I can access with the following jQuery code (from google chrome with --disable-web-security) 我有一项服务,可以使用以下jQuery代码(从带有--disable-web-security的谷歌浏览器)访问

$.ajax({
    type: 'POST',
    url: "http://10.30.1.2:9234/myapp/v6/token/generate",
    headers: {
        "Content-Type":"application/json",
        "Accept":"application/json"
    },
    data: JSON.stringify({ 
      "staffId" : "13254",
      "password" : "JustADummyPassword"
    })
}).done(function(data) { 
    console.log(data);
});

$.ajax({
    type: 'GET',
    url: "http://10.30.1.2:9234/myapp/v6/user/appl/Firstname/Lastname/email@address.com/1998-01-01",
    headers: {
        "Content-Type":"application/json",
        "Accept":"application/json"
    }
}).done(function(data) { 
    console.log(data);
});

The first call sets a cookie, which is required for the second call to authenticate. 第一个调用设置一个cookie,第二个调用进行身份验证需要该cookie。 This works fine, and both requests return expected results. 这可以正常工作,并且两个请求都返回预期结果。

I am trying to set up automated testing for the service, and have this written in JAVA, using RestAssured. 我正在尝试为该服务设置自动化测试,并使用RestAssured将其编写为JAVA。

public class UserApplication {
    public static Map<String, String> authCookies = null;
    public static String JSESSIONID = null;
    public static void main(String[] args) {
        Response resp = hello();
        resp = apiUserApplication();
    }

    public static Response apiUserApplication() {
        String userAppl = "http://10.30.1.2:9234/myapp/v6/user/appl/Firstname/Lastname/email@address.com/1998-01-01";

        Response response = RestAssured.given()
                .cookie("JSESSIONID", JSESSIONID).and()
                .header("Accept", "application/json").and()
                .header("Content-Type", "application/json").and()
                .when().get(userAppl);

        return response;
    }

    public static Response hello() {
        String helloUrl = "http://10.30.1.2:9234/myapp/v6/hello";

        Response response = RestAssured.given().cookies(authCookies)
                .contentType("application/json").when().get(helloUrl);

        return response;
    }
}

The first call (hello) works fine, and returns 200 code, and gets a valid token for use in the second call. 第一次调用(hello)可以正常工作,并返回200个代码,并获得一个有效令牌以供第二次调用使用。 The error I am getting from the second call with a 400 status code is... 我从第二个电话中收到的状态代码为400的错误是...

{"errors":["Content type 'null' not supported"]}

I'm not experienced with RestAssured , but your code is set up to first call, hello , then apiUserApplication . 我没有使用RestAssured经验,但是您的代码已设置为先调用,然后hello ,然后调用apiUserApplication You have some class level variables that you default to null , but you never explicitly give them a value. 您有一些默认为null类级别变量,但从未明确给它们提供值。 In particular, it looks in the second call, apiUserApplication , you are setting the value of the cookie to a null value. 特别是,它在第​​二个调用apiUserApplication ,您正在将cookie的值设置为null值。

I also do not see why you are returning this response object? 我也看不到您为什么要返回此响应对象? It would make sense if you were to examine it, ensure that the response has data as you expected and that you then set this session ID for the second request. 如果您要检查它,请确保响应中包含预期的数据,然后为第二个请求设置此会话ID,这将很有意义。

看起来JSESSIONIDauthCookies被初始化为null并且在此代码中未更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保证没有设置Content-Type - restassured is not setting Content-Type 在 Spring 中读取 Content-Type 应用程序/json - Reading Content-Type application/json in Spring 没有为Content-Type定义解析器:application / json - No parser defined for Content-Type: application/json RESTEasy:找不到内容类型应用程序/json 类型的编写器 - RESTEasy: Could not find writer for content-type application/json type MVC-当自定义内容类型时接受JSON(不是application / json) - MVC - Accept JSON when content-type is custom (not application/json) RestAssured:annot 确定如何序列化 content-type application/x-www-form-urlencoded。 如何使用键/值结构创建请求? - RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. How to create request with key/value structure? 使用 Fetch API javascript 将 Content-Type 设置为 application/Json - set Content-Type to application/Json using Fetch API javascript PlayFramework使用content-type application / json返回BadRequest - PlayFramework returns BadRequest with content-type application/json JAVA-处理针对Content-Type的HttpClient请求:application / json - JAVA - Process HttpClient request for Content-Type: application/json 我如何设置 Content-Type 以响应 application/json - How I can set Content-Type in react to application/json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM