I am trying to connect odoo and ionic 5 using api interface. Below is my Odoo controller:
@http.route('/ionicon/authenpost',type="json",csrf=False,cors="*", auth="public",website=False, method=['POST'])
def authenPost(self, login,pwd,base_location=None):
env = request.env(context=dict(request.env.context, show_address=False, no_tag_br=True))
db = env.cr.dbname
request.session.authenticate(db,login,pwd)
return request.env['ir.http'].session_info()
Deployment is ok and already tested with Postman. The response is ok as well.
But when I tried to call with ionic 5 android app, I got the following error:
{"status":400,"url":"http://172.20.10.3:8069/ionicon/authenpost","headers":{"date":"Mon, 18 Jan 2021 17:01:05 GMT","content-length":"308","server":"Werkzeug/0.16.1 Python/3.8.2","x-android-selected-protocol":"http/1.0","x-android-response-source":"NETWORK 400","x-android-received-millis":"1610989264019","x-android-sent-millis":"1610989263965","content-type":"text/html"},"error":"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p><function ionic_conController.authenPost at 0x7fc9687620d0>, /ionicon/authenpost: Function declared as capable of handling request of type 'json' but called with a request of type 'http'</p>\n"}
Actually I already pass 'application/json' header. Please see the below code:
this.http.sendRequest("http://172.20.10.3:8069/ionicon/authenpost", {
method: 'post',
responseType: 'json',
data:{ 'jsonrpc':'2.0', 'params' : { 'login':'testuser@sample.com', 'pwd':'testabc' }},
headers: {
accept: 'application/json',
'Content-Type': 'application/json'
}}).then(data=>{
console.log(JSON.stringify(data));
}).catch(err =>{
console.log(JSON.stringify(err));
});
I am using
import { HTTP } from '@ionic-native/http/ngx';
it did work with HTTPClient, below is my working code.
const httpHeader_ = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
let config ={ headers: new HttpHeaders().set('Content-Type', 'application/json') };
this.httpCli.post("http://172.20.10.3:8069/ionicon/authenpost",
{jsonrpc : "2.0", params : { 'login':'sampleuser@test.com', 'pwd':'password123' } },
config).subscribe(data => {
console.log(data);
},err => {
console.log(err);
});
I would like to know how can I get with HTTP?
Where can I put content-type ?
Best Rgds, jm