Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting null response from post request though request processed successfully

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

enter image description here

like image 728
R15 Avatar asked Dec 23 '22 19:12

R15


1 Answers

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);
});
like image 154
Spark Fountain Avatar answered Jan 20 '23 16:01

Spark Fountain