How to perform POST requests to a REST webservice?
Explaining the 405 error
You are performing a GET
request to an endpoint that only supports POST
requests. In this situation, the expected result is a 405
error:
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. [...]
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. 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.
To test a REST API, you should use the right tools. 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.
Just listing a few:
- cURL
- Postman (Chrome extension)
- Advanced REST Client (Chrome extension)
Admin
Updated on July 14, 2022Comments
-
Admin over 1 year
I have a RESTful webservice, in which I have written a method which receives
POST
requests.I am trying to access that method via the URL
http://localhost:8091/prjctname/post
.But its throwing the following error:
HTTP Status 405 - Request method 'GET' not supported
type: Status report
message: Request method 'GET' not supported
description: The specified HTTP method is not allowed for the requested resourceBelow 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"; } }
-
kjhughes over 5 yearsWhy hasn't this fine answer been accepted?