Mithilesh Kumar
Mithilesh Kumar

Reputation: 43

angular 4 post request not working, but postman is able to send post request to spring boot server?

I am able to successfully send a post request to my restcontroller in spring boot using postman. enter image description here

The restcontroller is as follows:

@RequestMapping(value="/performaction",method=RequestMethod.POST,consumes = "application/json", produces = "application/json")
    public void performReboot(@RequestBody PerformAction performAction) {
         System.out.println("......rebooting blade to performRebootservice...........for blade id :: : ::"+performAction.getBladeId() +performAction.getActionName());

 ..........
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*");
            }
        };
    }

However I am not able to send this post request from my angular, the request is as follows:

const headers = new HttpHeaders().set("Content-Type", "application/json");

    return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});

Note: I have added the WebMvcConfigurer from here!

Thanks.

Upvotes: 1

Views: 1291

Answers (2)

Pardeep Jain
Pardeep Jain

Reputation: 86730

return this.http.post(this.performactionUrl, {"bladeId" : 2,"actionName" : "reboot"},{headers});

should be -

return this.http.post(this.performactionUrl, JSON.stringify({"bladeId" : 2,"actionName" : "reboot"}),headers);

Upvotes: 0

baj9032
baj9032

Reputation: 2582

Without using subscribe method your api is never called. Just use subscription method in your component.

Upvotes: 3

Related Questions