Hello am facing a peculiar issue
I have enabled CORS on my Springboot API Server with following configuration
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
All my POST requests are working except an API for image upload. Its implemented as
@PostMapping(value = "/profiles/{id}/image")
@ResponseStatus(value = HttpStatus.CREATED)
public void uploadProfileImage(@PathVariable Long id, @RequestPart MultipartFile file) {
this.userService.uploadProfileImage(id, file);
}
On browser I see the OPTION for this request succeeding but the actual POST being made but hanging out and the console the displaying this error.
The error is
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:10000/users/profiles/1/image. (Reason: CORS request did not succeed).[Learn More]
The API does works correctly when used from PostMan so I think the issue is with CORS configuration and not actual API logic
Any pointers? Have tried adding @CrossOrigin to controller and specific API without any success.
Add this bean in your configuration to support CORS:
I found the issue. I am using angular 7 and angular http component. Had to change my post method from
to