在 PostMan 上发送发帖请求

发布于 2025-01-13 15:25:03 字数 609 浏览 1 评论 0原文


I have the following method I want to test using PostMan

public returnType name( String var1, String var2,String var3, String var4);

我尝试发送一个帖子请求来测试我发送了这样的一个,但它不起作用:

{
    "var1": "GU4777",
    "var2" : "HU 888",
    "var3" : "NU 8890",
    "var4" : "UJ 9909"
 }

我收到此错误:< br> 我收到此错误: 10:11:16.588 [http-nio-8080-exec-3] WARN org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper - javax.ws.rs.InternalServerErrorException:HTTP 500 内部服务器错误 ;


Can you guys please tell me the exact syntax I can use?

提前谢谢您

I have the following method I want to test using PostMan

public returnType name( String var1, String var2,String var3, String var4);

I tried to send a post request to test I sent one like this but it does not work:

{
    "var1": "GU4777",
    "var2" : "HU 888",
    "var3" : "NU 8890",
    "var4" : "UJ 9909"
 }

I get this error:
I get this error: 10:11:16.588 [http-nio-8080-exec-3] WARN org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper - javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error

Can you guys please tell me the exact syntax I can use?

Thank you in advance;

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

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

发布评论

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

评论(2

最美的太阳 2025-01-20 15:25:03

Rest API

为了完成您想做的事情,您首先需要一个 HTTP 服务器来侦听某个端点上的请求,我们将该端点称为 /test

然后,服务器在收到请求时必须解析 JSON 请求正文,提取参数,然后可以使用这些参数调用方法 name()

这里是一个使用 JavaScript 和 express.js 的服务器实现。对于这样的简单服务器,您不需要一个库(但为了简单起见,我在这里使用了一个库),并且您可以用几乎任何您喜欢的语言(例如 Java)来实现该服务器。
虽然另一种语言的语法会有所不同,但想法是相同的。

import express from "express";

// create app
var app = express();
// middleware to parse json
app.use(express.json())

// listen for requests on the /test endpoint which you will hit with Postman
app.post("/test", function (req, res) {
    // extract variables from json body
    const var1 = req.body.var1;
    const var2 = req.body.var2;
    const var3 = req.body.var3;
    const var4 = req.body.var4;
    // call method
    name(var1, var2, var3, var4);
    // send response with HTTP code 200
    res.status(200).send({
        "message": `Executed name() with var1 = ${var1}, var2 = ${var2}, var3 = ${var3}, var4 = ${var4}`  
    });
});

// the function that you want to call on the server
function name(var1, var2, var3, var4){
    console.log(var1, var2, var3, var4);

}

// start HTTP server and listen for requests on port 3000
const port = 3000;
app.listen(port, () => {
    console.log(`Listening on port ${port}`);
});

然后您需要启动您的服务器。在这种情况下,您可以通过执行 node server.js 来实现这一点,然后您可以使用 Postman 向服务器发送请求。
首先,将 JSON 有效负载(= 问题中的 JSON)放入请求正文中,并使用 POST 请求点击 localhost:3000/test 路由。然后,您应该会收到状态代码 200 的响应。

输入图片这里的描述

在服务器端你可以观察到这一点:
输入图片这里的描述

RPC/ gRPC

为了“直接”调用服务器上的函数,您可能需要查看RPCgRPC 适合您的首选语言。

Rest API

In order to do what you want to do you first need a HTTP server that listens for requests on a certain endpoint let's call that endpoint /test.

The server then must parse the JSON request body when it receives the request, extract the parameters and can then call the method name() using those parameters.

Here an implementation of a server like this using JavaScript and express.js. You do not need a library for a simple server like that (but I have used one here by reason of simplicity) and you can implement the server in pretty much any language you like e.g. Java.
While the syntax will differ in another language the idea will be the same.

import express from "express";

// create app
var app = express();
// middleware to parse json
app.use(express.json())

// listen for requests on the /test endpoint which you will hit with Postman
app.post("/test", function (req, res) {
    // extract variables from json body
    const var1 = req.body.var1;
    const var2 = req.body.var2;
    const var3 = req.body.var3;
    const var4 = req.body.var4;
    // call method
    name(var1, var2, var3, var4);
    // send response with HTTP code 200
    res.status(200).send({
        "message": `Executed name() with var1 = ${var1}, var2 = ${var2}, var3 = ${var3}, var4 = ${var4}`  
    });
});

// the function that you want to call on the server
function name(var1, var2, var3, var4){
    console.log(var1, var2, var3, var4);

}

// start HTTP server and listen for requests on port 3000
const port = 3000;
app.listen(port, () => {
    console.log(`Listening on port ${port}`);
});

You then need to start your server. In this case you do that by executing node server.js and then you can send requests to the server using Postman.
First, put your JSON payload (= the JSON from your question) into the request body and hit the localhost:3000/test route with a POST request. You should then receive a response with status code 200.

enter image description here

On the server side you can observe this:
enter image description here

RPC/ gRPC

In order to "directly" invoke a function on a server you might wanna have a look at RPC or gRPC for your preferred language.

小瓶盖 2025-01-20 15:25:03

我决定使用请求路径希望解决这个问题。

I decided to use request path wish solved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文