0

I am getting following error while calling post API.

405 Method Not Allowed

From angular i am calling post API like following.

private createFormUrl = `api/form/add`

public createForm(form: Form): Observable<string> {
    return this.httpClient.post<any>(this.createFormUrl, form)
      .pipe(catchError(this.handleError));
}

API in JAVA

@RestController
@RequestMapping( value = "/api/form")
public class FormManagementController {

private FormManagementService formmanagementService;

@PostMapping(path = "/add",
            consumes = MediaType.APPLICATION_JSON,
            produces = MediaType.APPLICATION_JSON)
    public ResponseEntity<?> createForm(@RequestBody(required = false) Form form) {
        String respond = formmanagementService.createForm(form);
        return ResponseEntity.status(getProperHttpStatus(respond)).build();
    }

Error:

URL: http://localhost/api/form/add (Working in postman)

enter image description here

I checked but, GET is not written any where for this API.

I checked many posts on stackoverflow but, nothing work for me. Any help would be greatly appreciated.

alfcope
  • 2,327
  • 2
  • 13
  • 21
ketan
  • 19,129
  • 42
  • 60
  • 98

4 Answers4

0

Try it in Postman collection to ensure its working

So you can isolate its a angular specific issue, You didn't share the request payload. If not workign in post man alos then make code changes.

Instead of @PostMapping try this @RequestMapping(value = "/add", method = RequestMethod.POST)

Full example Ref : Spring Boot 405 POST method not supported?

Senthil
  • 2,156
  • 1
  • 14
  • 19
  • `@RequestMapping("/backoffice/tags")` it's already there. Please check my question again. – ketan May 09 '19 at 10:45
  • Tried but, no luck. :( – ketan May 09 '19 at 11:33
  • Get rid of @Consumes and produces as well. Make use annotation RequestMapping instead of postmapping Ref accepted answer in this URL for a full example similar issue 405 not allowed: https://stackoverflow.com/questions/21706078/put-and-post-getting-405-method-not-allowed-error-for-restful-web-services?rq=1 – Senthil May 09 '19 at 13:53
  • Whether getting rid of @consumes solved the issue ? If not share, the JSON request payload as well. – Senthil May 10 '19 at 10:49
  • what is the request JSON which you are passing – Senthil May 17 '19 at 08:04
  • `{ "title": "test", "description": "test", "event": { "location": "place", "details": "test", "capacity": 10, "startDateTime": null, "endDateTime": null, "publishDateTime": null } }` – ketan May 17 '19 at 08:07
  • In one Project there are two container/module/service. Angular code in 1 module and Java API in another moudle/service. – ketan May 17 '19 at 08:08
  • Try to pass the headers in your angular code Ref : https://stackoverflow.com/questions/35032465/angular2-http-post-headers/35032846#35032846 . which angular version you are using – Senthil May 17 '19 at 08:23
  • I am using angular version 7. – ketan May 17 '19 at 08:25
0

You need to use MediaType.APPLICATION_JSON_VALUE to get the media type string value.

@PostMapping(path = "/add",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE) {
    ...
}
alfcope
  • 2,327
  • 2
  • 13
  • 21
  • Sorry but, no luck :( – ketan May 09 '19 at 11:37
  • @ketan Same error? Do you have any end-point working? Are you setting the content-type header when doing the request? Have you tried removing the requestbody parameter for testing? – alfcope May 09 '19 at 12:12
0

You better try with 'application/json' rather than MediaType.APPLICATION_JSON Not to defame anything, again. But there seems to be much topics upon the net about what should be used as the JSON MIME type.

The spec lists clearly only one MIME media type for JSON and it is "application/json" (see https://www.rfc-editor.org/rfc/rfc4627, section "6. IANA Considerations".

Just give a shot with the MIME type as 'application/json' in both consumes and produces.

@Robert

Community
  • 1
  • 1
0

It is clear that the response headers do not indicate that POST is allowed. The allow methods header should return something like GET, POST, PUT, DELETE, OPTIONS, HEAD'. Please add an attribute to the controller like so: @CrossOrigin(origins = "*", methods= "*"). methods is the property which will set the response header that lists which methods are allowed. Only browsers enforce CORS. Postman, curl don't enforce CORS. So, errors such as these are not evident at first glance. Hope that helps.

Prasad
  • 349
  • 2
  • 6