0
\$\begingroup\$

I have an API that I need to use that requires 2 things

  1. Basic auth with user: username & password: api token
  2. The request body must be form data and not JSON.

I am just checking my work here to make sure I am doing this right. Is this the correct way to implement this, is there a simpler way?

setAlert(params: SetAlertParams) {
    return this.http.get<{user: string, password: string}>(urls.getCredentials).pipe(
        switchMap(credentials => {
            const headers = new HttpHeaders({
                'Content-Type': 'application/json',
                'Authentication': `Basic ${btoa(`${credentials.user}${credentials.password}`)}`
            });
            const formData = new FormData();
            Object.keys(params).forEach(key => formData.append(key, params[key]));
            return this.http.post<SetAlertResult>(urls.setAlert, formData, {
                headers: headers,
                withCredentials: true
            });
        }
    ));
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.