cannot authenticate with API 400 bad request

cannot authenticate with API 400 bad request



I'm trying to authenticate the user with API. I have implemented a method for login function


login()

this.key = this.credentials.username + ":" + this.credentials.password;
this.base64 = btoa(this.key) //base64

this.url = encodeURI(this.base64); //url

this.contentHeader = new HttpHeaders().set("Authorization", "Basic " + this.url);

return new Promise((resolve, reject) =>
console.log(this.contentHeader)
this.http.post(this.LOGIN_URL, null, headers: this.contentHeader )
.subscribe(res =>
resolve(res);
console.log("success:" + res)
res => this.authSuccess(res.id_token)

, (err) =>

console.log("rejected");
reject(err);
);
);





I'm trying to concatenate username and password and then encoded to base64. again it encoded to URL and post with combining with the header. I'm getting authenticated properly with the postman correctly. but in the ionic app, it says bad request.





If you check in the developer tools network panel, does the header that's sent match the one you're sending in Postman?
– user184994
Sep 1 at 10:14





yes i checked, both the headers are maching
– Udara Indrajith
Sep 1 at 10:38




1 Answer
1



When you send a POST request using HTTP or HTTPClient, the second parameter is sent as the POST body. It seems in your code you're sending the Authorization not as a header, but as the POST body.



Change it to:


this.http.post(this.LOGIN_URL, null,
headers: HttpHeaders().set("Authorization", "Basic " + this.url)
)





I change it to null but the same problem arise
– Udara Indrajith
Sep 1 at 12:21





@UdaraIndrajith Can you update your question with the latest code then please? I'll take a look. When you sent in Postman, what did you send in the body?
– user184994
Sep 1 at 12:23






nothing in the body. only header with the username and the password
– Udara Indrajith
Sep 1 at 12:28



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế