How to create Http.get request in angular2 ionic2?


#1

Hi I have the following details … How to make Api request in angular2

URL : http://www.taskbean.com/api/user/login

postdata:{“UserName”:“someemail@com”,“Password”:“sdasdasd”}

Method GET


#2

Here’s an example, I’ll let you modify it for your needs. You can’t send a body of data with a GET request in Angular 2, you will have to use POST.

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

	    let data = {
	    	username: this.username,
	    	password: this.password
	    };

	    this.http.post('http://yoururl', JSON.stringify(data), {headers: headers})
	      .subscribe(res => {
	      	console.log(res.json());
	      }, (err) => {
	      	console.log(err);
	      });

#3

Thank u … for the reply … @joshmorony


#4

Hi,

I have tried with your given method but, i have not got the result, I have tried Like :-

	let NamePass : {
		'username' : 'xyz',
		'password' : 'xyz'
	}

where, username and password is the field name

	this.http.post(URL, JSON.stringify(NamePass), {headers: headers}).subscribe(data =>	console.log('Fetched :- ',data), err => console.log('Error :- ',err));

Thanks


#5

Please tell me what error you are getting , or Post Log?


#6
  Error :-  Object { _body: error, status: 0, ok: false, statusText: "", headers: Object, type: 3, url: null }

I am not getting the data from that URL
(like :- http://abc.xyz.com/!!!/!!!/xyz.php?XTYPE=2&YTYPE=1&page=0 ) (protected with ID & Password)

I am getting the the result correctly, when the URL is not protected with username & password.

Can you share the working code with us, if it is possible ?

Thanks


#7

I also stuggled for the same issue and now i fixed it please try this code,this vl work for sure

 isAuthenticated: boolean = false;


        var headers = new Headers();
        var creds = {username:"someemail@com",password:"sdasdasd"};
        headers.append('Content-Type', 'application/json');
        return new Promise((resolve) => {
            this.http.post(`http://www.taskbean.com/api/user/login`, creds, { headers: headers }).subscribe((data) => {
                    this.isAuthenticated = true;
                    resolve(this.result);
            },
            (error) => { 
                this.result = {status:this.isAuthenticated,error:error.json().error};
                resolve(this.result)
            })

        })

based on is authenticated u can navigate to different page


#8

Try this,

import ‘rxjs/add/operator/map’;
import {Http, Headers,Response, RequestOptions} from “@angular/http”;

postData(){
let postParams = {
user_name:“username”,
password: “password”
}
var headers = new Headers();
headers.append(“Accept”, ‘application/json’);
headers.append(‘Content-Type’,‘application/json’);
let options = new RequestOptions({ headers: headers });

this.http.post('http:www.test.com/api/login',postParams,options)
.map((res:Response) => res.json());

}


#9

Hy All … i m suffering a huge problem …please someone help me @joshmorony

My url is http://139.162.61.147:29016/intpart/api/dataElementGroupSets
it redirect to login page : http://139.162.61.147:29016/intpart/dhis-web-commons/security/login.action

my user password is : dilroop,Dilroop@123

it gives this error " Response for preflight is invalid (redirect)"

Please help me… i want to fetch data fro api…


#10

That sounds like a CORS problem to me. You also want to make sure you have a proper SSL certificate and are using https endpoints, not http. That’s important for security in general, but especially so if you’re targeting iOS, because you may find your requests not working at all in deployment.


#11

hy @rapropos thanks for replying…i tried using https endpoints …but still its not working…
here is the code https://github.com/Dilroop123/http_request

please help me…i have spent many days and my deadline is soon…
please help me in getting api data …