POST request usually works fine, occasionally it fails with 400 error

28 Views Asked by At

I have developed a Post API using Python, and I consume it with Angular 7. My issue is that while this POST request usually works fine, occasionally it fails and shows me a bad request error with a status code of 400.

 onRedirectClick() {
    const formData = new FormData();
    const date = new Date();
    formData.append("date", date.toISOString().replace("T", " ").split(".")[0]);
    formData.append("redirection", environment.redirection);
    formData.append("msg", this.createUUID(date));

    this.userService.redirectHmac(formData).subscribe(
      (data) => {
        this.userService
          .redirectUrl(formData, data.hmac, data.token)
          .subscribe(
            () => {
              window.open(`${environment.Url}${environment.redirection}`);
            },
            (failed) => {
              console.error("redirection : ", failed);
            }
          );
      },
      (failed) => {
        console.error("hmac redirection : ", failed);
      }
    );
  }

///////////////////////////////////////////

  redirectHmac(formData: FormData): Observable<{hmac: string, token: string}> {
    return this.http.post<{hmac: string, token: string}>(`${environment.apiUrl}/api/redirection`, formData);
  }

  redirectUrl(formData: FormData, hmac: string, token: string) {
    const httpHeaders = new HttpHeaders()
      .set('Authorization', 'Bearer ' + token)
      .set('Accept', 'text/html')
      .set('X-HMAC-TOKEN', hmac)
      .set('X-MESSAGE-ID', formData.get('msg') as string);

    formData.delete('msg');
    return this.httpClient.post(`${environment.apiLoginUrl}`, formData, {
      headers: httpHeaders,
      responseType: 'text',
      withCredentials: true,
    });
  }

NB: this issue is occured with the second request,

Could someone help me, Thanks in advance

I have tested this api with postman and it works fine

I know a 400 erro means the request was malformed but it usualy works,

0

There are 0 best solutions below