I have an authenticate API on springboot server. When I call the API from POSTMAN, the userDetails object is not null and I get success on client side. However when I do from reactJS userDetails object is null and server returns 500 with msg Response for preflight does not have HTTP ok status.
Could someone help me explain why its successful in POSTMAN and not in javascript?
Code in ReactJS
var data = {
"password":"password",
"username":"uname"
}
fetch("https://ipaddress:port/module/authenticate", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
}).then(response => {
...
})
.catch((e) => {
...
})
Code in springboot
@PostMapping(value = "/module/authenticate ")
@ResponseBody
public ResponseEntity<String> authenticate(@RequestAttribute UserDetails userDetails) throws Exception {
...
}
public class UserDetails {
// The username for the logging in user.
private String username;
// The password for the user.
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Found Reason for POST man not failing
Post man doesn't do an OPTIONS call before the POST API hence, it'll not fail. However the fetch call does OPTIONS check first for cors access, and it needs to be handled by spring security. Reference: Why is an OPTIONS request sent and can I disable it?
A work around could be to set content type as plain/text