HTTP POST No working with typescript?

57
0
03-29-2020 07:49 AM
Highlighted
New Contributor

I'm trying to reproduce a post that works on the postman in the angular, however, when I send the header it gives an error, however, I make a request to the same server to obtain the token without adding the header and it works

image

Try send post in angular and return message:

enter image description here

This method get an Token, work fine, look not send header...

async getTokenWorkForce(){    return this.loginWorkForce();}     async loginWorkForce(): Promise<any> {        return new Promise(async (resolve, reject) => {            const username = Environment.username_workforce;            const password = Environment.password_workforce;             let formData: FormData = new FormData();             formData.append('referer', this.apiServiceProvider.URL_OS_ORDEMSERVICO_WF_TOKEN);            formData.append('username', username);            formData.append('password', password);            formData.append('client', 'referer');            formData.append('f', 'pjson');             this.http.post(this.apiServiceProvider.URL_OS_ORDEMSERVICO_WF_TOKEN, formData)                .subscribe((response) => {                    const json = response.json();                    this.loggedUserTokenWF = `${json.token}`;                    resolve(this.loggedUserTokenWF);                });                     })    }

This method return error, with header…

    public async postWorkForceFindAssigmentOS(nroOS: number, outFields: string😞 Promise<any> {        return new Promise(async (resolve, reject) => {            const url = this.apiServiceProvider.URL_OS_ORDEMSERVICO_WF_GET_ASSIGNEMENTS;             const formData = new FormData();             formData.append('outFields', `${outFields}`);            formData.append('where', `workorderid='${nroOS}'`);            formData.append('Units', 'Feet');            formData.append('returnGeometry', 'pjson');            formData.append('f', 'false');            await this.authProvider.getTokenWorkForce();             let header = new HttpHeaders();             console.log('Token WF: ' + this.authProvider.loggedUserTokenWF);            header = header.set('Authorization', `Bearer ${this.authProvider.loggedUserTokenWF}`);             this.httpServiceProvider                 .postWorkForce(header, url, formData)                .subscribe(response => {                    console.log(response);                    resolve(response);                }, (err) => {                    LogHelper.logError(err);                    reject(err)                });        });    }     postWorkForce(        headers: HttpHeaders,        url: string,        model: any,        times: number = 3    ) {        return this.http.post(            url,            model,            {                headers: headers             }        ).pipe(retry(times));    }
Tags (1)
Reply
0 Kudos
0 Replies