API请求使用Java与剧作家失败

发布于 2025-02-05 02:02:24 字数 1379 浏览 2 评论 0原文

我正在尝试使用Java学习用于API和Web测试的剧作家。到目前为止,通过API测试,我会遇到一个奇怪的问题,在我的所有请求中都会遇到以下错误:

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

这是我的代码:

        Playwright playwright = Playwright.create();
        APIRequestContext request;
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        request = playwright.request()
                .newContext(new APIRequest.NewContextOptions()
                        .setBaseURL("https://petstore.swagger.io/v2/store")
                        .setExtraHTTPHeaders(headers)
                );
        Map<String, String> body = new HashMap<>();
        body.put("id","400");
        body.put("petId","731368479");
        body.put("quantity", "4");
        body.put("status","placed");
        body.put("complete", "true");
        APIResponse addNewPet = request.post("/order",
                RequestOptions.create().setData(body));
        System.out.println(addNewPet.text());
//        assertTrue(addNewPet.ok());
//        assertEquals(200, addNewPet.status());

        playwright.close();

我不确定在哪里和在做什么错。提前致谢。

I am trying to learn Playwright for api and web testing using Java. So far with api testing I am getting a weird issue where I am getting the following errors for all of my requests:

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

Here is my code:

        Playwright playwright = Playwright.create();
        APIRequestContext request;
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        request = playwright.request()
                .newContext(new APIRequest.NewContextOptions()
                        .setBaseURL("https://petstore.swagger.io/v2/store")
                        .setExtraHTTPHeaders(headers)
                );
        Map<String, String> body = new HashMap<>();
        body.put("id","400");
        body.put("petId","731368479");
        body.put("quantity", "4");
        body.put("status","placed");
        body.put("complete", "true");
        APIResponse addNewPet = request.post("/order",
                RequestOptions.create().setData(body));
        System.out.println(addNewPet.text());
//        assertTrue(addNewPet.ok());
//        assertEquals(200, addNewPet.status());

        playwright.close();

I am not sure where and what I am doing wrong. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2025-02-12 02:02:24

如果您在baseurl的末端添加斜线并将其从邮政URL中删除,则似乎正在工作:

...
.setBaseURL("https://petstore.swagger.io/v2/store/")
...
APIResponse addNewPet = request.post("order",
...

Seems to be working if you add the slash at the end of the baseURL and remove it from the post url:

...
.setBaseURL("https://petstore.swagger.io/v2/store/")
...
APIResponse addNewPet = request.post("order",
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文