简体   繁体   English

如何对 REST 网络服务执行 POST 请求?

[英]How to perform POST requests to a REST webservice?

I have a RESTful webservice, in which I have written a method which receives POST requests.我有一个 RESTful web 服务,我在其中编写了一个接收POST请求的方法。

I am trying to access that method via the URL http://localhost:8091/prjctname/post .我正在尝试通过 URL http://localhost:8091/prjctname/post访问该方法。

But its throwing the following error:但它抛出以下错误:

HTTP Status 405 - Request method 'GET' not supported HTTP 状态 405 - 不支持请求方法“GET”
type: Status report类型:状态报告
message: Request method 'GET' not supported消息:不支持请求方法“GET”
description: The specified HTTP method is not allowed for the requested resource描述:请求的资源不允许指定的 HTTP 方法

Below is the code snippet.下面是代码片段。

@RequestMapping(value = "/post", method = RequestMethod.POST)
@Produces("application/json")
@ResponseBody 
public String createUser(@RequestBody PCDBean bean, HttpServletRequest request) {

    String username = null;
    String password = null;
    System.out.println("inside post method");

    String result = service.getCustomer(username, password);
    if(result != null) {
        return username + password;
    } else {
        return "failure";
    }
}

Explaining the 405 error解释 405 错误

You are performing a GET request to an endpoint that only supports POST requests.您正在向仅支持POST请求的端点执行GET请求。 In this situation, the expected result is a 405 error:在这种情况下,预期结果是405错误:

6.5.5. 6.5.5. 405 Method Not Allowed 405 方法不允许

The 405 (Method Not Allowed) status code indicates that the method received in the request-line is known by the origin server but not supported by the target resource. 405 (Method Not Allowed) 状态码表示在请求行中接收到的方法被源服务器知道但不被目标资源支持。 [...] [...]

Picking the right tool选择正确的工具

I guess you are trying to access the endpoint using the browser address bar .我猜您正在尝试使用浏览器地址栏访问端点。 However, when typing the resource URL in the browser address bar, you are performing a GET request.但是,在浏览器地址栏中键入资源 URL 时,您正在执行GET请求。 That's the default behavior of the browser.这是浏览器的默认行为。 Deal with it .处理它

Hence, the browser address bar won't give you much flexibility to test a REST API, once you only can perform GET requests from it.因此,浏览器地址栏不会给你太多的灵活性来测试 REST API,一旦你只能从它执行GET请求。

To test a REST API, you should use the right tools .要测试 REST API,您应该使用正确的工具 They will give you the ability to perform requests using all the HTTP methods (including POST ), set request headers and other features that are not available in the browser address bar.它们将使您能够使用所有 HTTP 方法(包括POST )执行请求、设置请求标头和浏览器地址栏中不可用的其他功能。

Just listing a few:简单列举几个:

The real issue in my case was that my text wasn't exactly the one expected in the server.在我的案例中,真正的问题是我的文本并不完全符合服务器中的预期。 The object had a spelling error.对象有拼写错误。 Should be exactly "{\\n"+"\\"jobName\\":\\"MOVE_TO_PICK_POSITION\\"\\n"+"}";应该正好是 "{\\n"+"\\"jobName\\":\\"MOVE_TO_PICK_POSITION\\"\\n"+"}"; and I was using something else.我正在使用其他东西。

The 405 error message made me waste a lot of time since it made me think that there was some problem with my POST request. 405 错误消息让我浪费了很多时间,因为它让我认为我的 POST 请求有问题。

Hope this helps.希望这可以帮助。

You need to get the browser to accept a post request, like this:你需要让浏览器接受一个 post 请求,像这样:

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login Page</title> </head> <body> <form action="login" method="post"> name: <input type="text" name="name"> <br> password: <input type="password" name="password"> <br> <input type="submit" value="submit"> </form> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM