[英]Avoid 406 error when using GET method in API testing with Cypress
I would like to test the api of an application, I've got a token from the developer, but I'm getting an error 'cy.request() failed on:我想测试一个应用程序的 api,我从开发人员那里得到了一个令牌,但我收到错误“cy.request() failed on:
https://****************************** https://******************************
The response we received from your web server was:我们从您的 web 服务器收到的响应是:
406: Not Acceptable 406:不可接受
This was considered a failure because the status code was not 2xx or 3xx.'这被认为是失败,因为状态代码不是 2xx 或 3xx。 This is the code that I used:这是我使用的代码:
describe('API Test', () => {
const token = 'your_token_here';
it('Should make a GET request with a Bearer token and avoid 406 error', () => {
cy.request({
method: 'GET',
url: 'https://your-api.com/endpoint',
headers: {
'Authorization': `Bearer ${token}`,
'Accept': 'application/json'
}
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body).to.be.an('object');
});
});
});
I tried with 'Accept': 'application/json' and 'Content-Type': 'application/json', but it didn't help.我尝试使用 'Accept': 'application/json' 和 'Content-Type': 'application/json',但没有帮助。
the Content-type header is used to indicate the original media type of the resource. Content-type header 用于表示资源的原始媒体类型。 Here since you are sending a GET request content-type
does not matter.在这里,因为您发送的是 GET 请求content-type
无关紧要。
the Accept header is used by the sender of the request(browser) to specify response media types that are acceptable.请求的发送者(浏览器)使用Accept header 来指定可接受的响应媒体类型。 So here in your case maybe the type of the response
is not application/json
that's why you are gettin an因此,在您的情况下, response
的类型可能不是application/json
这就是为什么您要
406: Not Acceptable 406:不可接受
I found also in this source that in some cases, the server could be responsible for the 406 Error since it's the.network object producing the error.我还在这个来源中发现,在某些情况下,服务器可能对 406 错误负责,因为它是 .network object 产生错误。 Perhaps the server is misconfigured and can't handle the request correctly.可能服务器配置错误,无法正确处理请求。 Maybe it's a traffic routing issue.也许这是一个流量路由问题。
so try your REQUEST without any header and if the 406 still occurs you are sure that it is from ther server and that your REQUEST is OK.因此,请在没有任何 header 的情况下尝试您的 REQUEST,如果 406 仍然出现,您确定它来自那里的服务器并且您的 REQUEST 是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.