[英]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,这将很有意义。
看起来JSESSIONID
和authCookies
被初始化为null
并且在此代码中未更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.