NanoHTTPD服务器-POST请求

时间:2020-04-22 12:43:49

标签: java post request backend nanohttpd

我正在尝试使用NanoHTTPD作为库来创建用于处理帖子请求的HttpServer。这是我的代码:

public class ServerPanel extends NanoHTTPD {

    public ServerPanel() throws IOException {
        super(8080);
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
        System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
    }

    @Override
    public Response serve(IHTTPSession session) {

        if (session.getMethod() == Method.POST) {
            try {
                session.parseBody(new HashMap<String, String>());
                String requestBody = session.getQueryParameterString();
                return newFixedLengthResponse("Request body = " + requestBody);
            } catch (IOException | ResponseException e) {
                e.printStackTrace();
            }
        }
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "The requested resource does not exist");

    }
}

我尝试使用邮递员发送发帖请求,这就是我得到的:postman screenshot
我做错了什么?我想从发帖请求中获取用户名和密码,但我会得到null。

谢谢!

0 个答案:

没有答案