I am trying to get data from a rest api call and with python request and it is working. How can I do similar in JavaScript and Typescript? I will need it in my Angular2 application.
Working Python script import requests
url = "restapi"
r = requests.post(url, auth=('user', 'password'))
print(r.text)
You need to use http (provided with RxJS)
Your import:
import { Http } from '@angular/http';
Injected in your constructor:
constructor(public http: Http) {}
Your rest request method:
login(user:User,onNext: (response) => void) {
this.http
.post(BASE_URL+"/login",user)
.map(response => response.json())
.subscribe(onNext,(e)=> console.error(`${e}`));
}
Note1: User is a typed json with just {user:string, password:string}
Note2: Please consider to do all the above in a @Injectable() Provider/Service, and the next step in your component (for beginning you can make everything in the same class)
Then use it like this
login(myUserJson,(result)=>{
console.log(result);
//proceed!...
})