I'm doing an Http request in Angular 4 using its builtin HttpClient class. Everything works well, but there is a small problem with the error variable returned.
When I post the wrong username/password combination to login the user, the error object returned contains an error key for which the value is expected to be an Object but I'm getting a string instead. Please see below the text between ** in the object to understand what's the issue:
{**error: "{"non_field_errors":["Unable to login with provided credentials."]}"**, headers: Object, status: 400, statusText: "Bad Request", url: "http://mymedicine.loc/api/auth/login/", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://project.loc/api/auth/login/: 400 Bad Request"}
What might cause this kind of problem? I need to have an object there just so I can let the user know what went wrong during the authentication.
I need to precise that the authentication works well when the correct credentials are provided and that I have activated CORS on the webserver I'm sending the HTTP request to.
Below is a short piece of code extracted which does the POST http request:
interface ErrorMessage {
username: string[1];
password: string[1];
non_field_errors: string[1];
}
interface ErrorResponse {
error: ErrorMessage;
}
// get token from the server
this.http.post<TokenResponse>('http://myproject.loc/api/auth/login/', body).subscribe(
(res: TokenResponse) => {
// login with token
this.auth.login(res.auth_token);
// redirect
this.router.navigateByUrl('/url');
},
(err: ErrorResponse) => {
let error = err.error;
console.dir(err);
}
);
curlon the command line is fine (i.e. a json object)