My code seems simple enough.
let body = new FormData();
body.set("a", true);
body.set("username", this.user);
body.set("password", this.password);
let headers = new HttpHeaders();
headers.set("Content-Type", 'application/x-www-form-urlencoded')
console.log("about to post");
this.http.post("/endpoint", body, {headers: headers}).subscribe(
(result) => { console.log("success", result); /*extra code*/},
(error) => { console.log("error", error); /*extra code*/ },
() => { console.log("Complete"); /*extra code*/ });
This is a rather straightforward subscription process.
When I make a post request, it returns a 401 unauthorized network request error. The problem lies in the fact that the console output displays
about to post
POST [url] 401 (Unauthorized)
Complete
It doesn't execute the error handling functionality.
In the constructor, http is defined as: private http: HttpClient
Therefore, I am confused about why it fails to trigger the error response. Is there something missing? I checked similar posts like Catching errors in Angular HttpClient which I believed I followed regarding the subscription process.
On successful requests, the success function works fine.
Edit Upon further inspection of the code, I realized that maybe the data or headers were incorrect. So, I made updates accordingly. It seemed that the data being sent appeared incorrect, so I changed it to form data. As I traced through the endpoint until encountering a "throw new Exception('Authorization Error')", the error function was never triggered. Could there be something lacking in how I set up the post request? Perhaps an internal issue caused by improper configuration?