简体   繁体   English

在使用 Cypress 进行 API 测试时使用 GET 方法时避免 406 错误

[英]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.

相关问题 用json响应时出错406 - 406 error when responding with json 通过 RStudio 使用 R API 的 Stats Canada REST 服务器和 JSON 出现 406 错误 - 406 error with Stats Canada REST server and JSON using R API via RStudio 如何在jQuery中获取406错误(不可接受) - How to get 406 Error (not acceptable) in jquery 将JSON发送到Spring Controller时出现406错误 - 406 error when sending JSON to Spring Controller 尝试在Java Spring中获取JSON时为什么会出现406错误? - Why am I getting a 406 error when trying to get JSON in Java Spring? 使用Groovy以相同方法对基于REST的API测试使用GET和PUT的有效方法 - Effective way to use GET and PUT in the same method for REST based API Testing using Groovy 为什么在尝试使用file_get_contents()函数时收到PHP错误406,以及如何解决? - Why do I get a PHP error 406 when trying to use file_get_contents() function and how to solve it? 使用PHPUnit在Laravel中测试Restful API时出错 - Error testing Restful API in Laravel using PHPUnit 406返回JSON对象时出错 - 意外内容 - 406 Error when returning JSON object – Unexpected content 尝试使用Java运行sql查询时出现406错误 - 406 error when attempting to run a sql query with java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM