0

I need to make a http get request with a basic authentication, but I do not know how. I am using ionic2, angular & typescript. My code:

    this.http.get("http://address")
     .map(res => res.json())
     .subscribe( data =>{
        console.log(data);
  });

From the Ionic doc I see what I need, but I am new to these technologies and I am not able to use it: useBasicAuth(username, password)

I have tried these solutions, but it did not work: Angular 2- Http POST request parameters

EDIT

I have been trying the code below

let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Basic xxxxxxx=');

let options = new RequestOptions({ headers: headers });

this.http.get("http://VLCwebServerAddress",options)

or even

this.http.get("http://VLCwebServerAddress",headers)

If the second parameter is headers I get the response 401, so the Auth failed.

If the second parameter is options I get Response for preflight has invalid HTTP status code 501 which I've read it's related with CORS in the server side.

Borja Perez
  • 3
  • 1
  • 7

4 Answers4

0

You need to store the username and token in sessionstorage or localstorage when the login in success. then you need to send them in the header of your request.

getUserByName(username: string) {
let url="http://localhost:8088/rest/user/userName";
let header=new Headers({'Content-Type': 'application/json', 'Authorization': localStorage.getItem("token")});

return this.http.post(url, username, {headers: header});

}

Praneeth Reddy
  • 424
  • 3
  • 7
  • I got "cannot find getUserByName" if I try your code. I'd like to know how can I use "getBasicAuthHeader(username, password)" which is defined in the docs, but without example of usage. – Borja Perez Jun 20 '17 at 18:29
  • Do you have any idea on basic programming? The code I wrote need to keep in service. – Praneeth Reddy Jun 20 '17 at 22:24
  • I easily get stuck... Nevertheless I think I've understood your code and I tried to implement as you could see in the edit. But still without success. Thnx. – Borja Perez Jun 21 '17 at 01:48
0

Just simply append a header;

let headers = new Headers();
headers.append('Authorization', 'Basic' + btoa('username' + ":" + 'password'));
this.http.get(url, { headers: headers });
Sahil Daga
  • 401
  • 4
  • 6
0

I eventually got it. I was mixing-up angular and Ionic2-cordova libraries.

I didn't notice that I had to use import { HTTP } from '@ionic-native/http';

instead of using import { Http, HttpModule } from '@angular/http';

(which was the code I've seen in tutorials and other questions) then, I am able to used all the methods that are listed in the http documentation for Ionic2

Bear in mind that this plugin won't work while running ionic serve yourApp on the browser, but while running the app on a real device.

I've re-checked and tried several times doing the request with the http angular library but as soon as I add extra content like headers to the request, it stops working (not sure if it is because of the server). Thank you for the answers.

Borja Perez
  • 3
  • 1
  • 7
0

Along with your RequestOptions add the following parameter

withCredentials: true
stathis
  • 11
  • 3