2

My Web API method returns string and I am trying to use that string in Angular application. I use swagger and have no issues from Web API side. It is showing string in Swagger

 [HttpPut]
    [Route("UpdateUserStatus")]
    [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
    public async Task<IActionResult> UpdateUserStatus(User user)
    {
        string userUpdated = await _userService.UpdateUserStatus(user);

        return Ok(userUpdated );
    }

I am trying to access this string in angular. Service returns like this

public UpdateUserStatus(user: User): Observable<any> {
   const url = this._url + 'UpdateUserStatus';
   return this._httpClient.put<any>(url, user);
}

 this._service
  .UpdateUserStatus(this.User)
  .subscribe((response: any) => {
    this._notificationService.info(
      response, //this is not working
      'User updated'
    );
  }, (error) => {
    this._rapNotificationService.error(
      'Update Failed. ',
      'Failed'
    );
  });

It is giving "http failure during parsing for string" error.

Chatra
  • 2,989
  • 7
  • 40
  • 73

1 Answers1

0

You can specify that you need the string response and not json (which is the default)

public UpdateUserStatus(user: User): Observable<any> {
   const url = this._url + 'UpdateUserStatus';
   return this._httpClient.put<any>(url, user,{responseType: 'text'});
}
Alvin Saldanha
  • 886
  • 9
  • 20
  • I specified it but it is giving error message that responsetype should be json and cannot convert type json to string https://stackoverflow.com/questions/50798592/angular-6-how-to-set-response-type-as-text-while-making-http-call – Chatra Jun 25 '19 at 22:16
  • Type '"text"' is not assignable to type '"json"'.I doubt it is because of put. I see responsetype text working for get, I may be wrong – Chatra Jun 26 '19 at 00:52