I am hitting a post
request using httpClient
, but getting null
response though request processing Successfully.
serviceClass.ts
file
this.httpOptions = {
headers: new HttpHeaders(
{
'Content-Type': 'application/json; charset=utf-8',
'tenant-code' : 'VdoFWiHL8RFR8fRGNjfZI=',
'Authorization': 'Basic ' + btoa('pass:username')
})
}
public Reprocess(ObjProduct) {
var Jobj=JSON.stringify(ObjProduct);
return this._http.post(this.ReprocessUrl,Jobj,this.httpOptions)
}
When I am calling the above method in Component, I'm getting null
response from API.
Component Code
var op = this.objService.reprocess(this.reprobj);
console.log("output: ", op);
Here op
is completely incomprehensible, it's showing _scaler=false
etc. How can I get correct status of the service call?
Edit 1: When I make same request from postman getting status Ok 200
.
Edit 2: Below code also giving null
result(as per the @Spart_Fountain's answer)
var op= this.restApi.Reprocess(this.reprobj).subscribe((data:any) => {
console.log("data "+ data);
});
Postman header screenshot
The reason why you get a "strange looking" response when calling this.objService.reprocess(this.reprobj);
is that this method will return a Subscription object.
In detail: The method reprocess
returns a subscription to an observable, because the method subscribe()
is called inside of the return
statement itself. What you would rather do is to return only the observable and subscribe to it outside of the reprocess
method:
public reprocess(objProduct) {
var objParam = JSON.stringify(objProduct);
return this._http.post(this.url, objParam, this.httpOptions);
}
var op = this.objService.reprocess(this.reprobj).subscribe(resp => {
console.log("resp from api service: " + resp);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With