Hi im new to ionic and angular a lot of things i still didnt know, i want to send post request to api with basic auth. heres my code :
import { Component } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http'
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(public http:HttpClient) {
this.call();
}
call(){
var data = {username:"someusername",password:"somepassword"}
let body = JSON.stringify(data);
let header= new HttpHeaders({
'Accept': 'application/json',
'Method': 'POST',
'Content-Type': 'application/json',
'Authorization':'Basic ZHJlYW1fMS4wOmRyZWFtXzEuMA==',
});
let options = {headers: header}
this.http.post("someUrl", body, options).subscribe(data =>{
console.log(data)
}, error => {
console.log(error);
});
}
}
im following tutorial from this link : “Ionic 5 HTTP POST with Angular by Example | Techiediaries”, but look like im using the new version ionic, so i change a bit by following this post “angular6 - angular 6 or 7: How to import RequestOptions and ResponseContentType in '@angular/common/http' - Stack Overflow”. but i get error like this:
then i realize, look like my headers not added to the url. here the image from consolelog.(option) header i declare :
and heres image from console.log(error):
so my question is, how to add header i declare to the url ?, is the header added to url will solved this problem ?, or maybe i miss something ?.
thank you in advance, and sorry for bad grammer english.
1 post - 1 participant