Http request with angular 6

Asked

Viewed 449 times

0

I’m trying to consume a REST service that has a basic authentication with angular 6 but I’m not succeeding follow the code I’m using unsuccessfully:

public iniciarProcesso(dadosProcesso: string): Observable<any> {
    const headers = new Headers()

    headers.append('Content-Type', 'application/json')
    headers.append('Authorization', 'Basic YWRtaW46YWRtaW4=')

    return this.http.post(
        this.API,
        dadosProcesso,
        {headers: headers}
    ).pipe(map((res) => {
        console.log(res)
        return res
        })
    )
}

I receive in response:

OPTIONS http://localhost:8080/engine-rest/process-definition/key/agendarEventoTeatro/start 401 (Unauthorized)

Failed to load http://localhost:8080/engine-rest/process-definition/key/agendarEventoTeatro/start: Response for preflight does not have HTTP ok status.

Does anyone have any light on how to make this requisition?

NOTE: I was able to make it work by creating a proxy file in my project and starting Node (ng serve) with --proxy solution (Gambiarra) taken from this site [https://medium.com/@gigioSouza/solving-the-Cors-problem-with-angular-2-e-o-angular-cli-7f7cb7aab3c2] My question is whether I publish my project on the Apache server or any other will work that "Gambiarra" on localhost worked well.

  • Apparently it is your server that is rejecting the connection, (it seems by permission) if you can post your server controller here it would be easier to understand the problem

  • I do not have access to the controller code only of the server and web settings that in the case is Wildfly version 8

  • This is not angular problem, you’re making a request and it works, but on the other side there is a server that performs authentication and you’re not going through it because it has a 'Authorization', 'Basic YWRtaW46YWRtaW4=' tied to the code. That’s not how it works. You need to log in and save the Basic in place Torage to reuse it.

  • Have an example of how to do this of storing the login in the Angular Path

1 answer

-1

I started using Angular 6 not long ago but in this type of request use usually this way:

func(objetoEnviado): Observable<TipoDoObjetoEnviado>{
    this.http.post<TipoDoObjetoEnviado>(linkDaApi, objetoEnviado)
}

Using a variable type reference sent. I don’t know how your application is structured but this can help.

  • So I can consume good other services that have not implemented a security, in this case the service Rest was implemented with a basic authentication that should be passed by the header but I am not succeeding, tested with Postman and the connection occurred successfully.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.