POST 2 Guid parameters to API with Angular

I Have made an API to follow a user. The method accepts 2 parameters which are two guids. The method:

// Follow user
[HttpPost]
public async Task<ActionResult<Guid>> FollowUser([FromBody] Guid user_gd, Guid user2_gd)
{
  if (ModelState.ErrorCount > 0)
  {
    return BadRequest();
  }
  var followedUser = await _user.FollowUser(user_gd, user2_gd);
  return Ok(followedUser);
}

The manager in API:

public async Task<bool> FollowUser(Guid user_gd, Guid user2_gd)
        {
            var followUserQuery =
                @"
                    insert into userbind(gd, user_gd, followed_user_gd, date_followed) 
                    values(@_gd, @_user_gd, @_followed_user_gd, @_date_followed)
                ";
            await PostQuery(followUserQuery, new
            {
                _gd = GenerateGd(),
                _user_gd = user_gd,
                _followed_user_gd = user2_gd,
                _date_followed = DateTime.Now
            });

            return true;
        }

The API request in Angular (service):

followUser(followed_user, user_gd): Observable<any> {
    try {
      return this._http.post<any>(this._apiUrl + "FollowUser", { "user2_gd": followed_user, "user_gd": user_gd }, this.httpOptions);
    } catch (e) {
      console.log("POST error: ", e);
    }
  }

The component:

followUser(gd) {
    console.log(gd);
    this._userService.followUser(gd, localStorage.getItem("gd")).subscribe(
      res => {
        console.log(res);
      }
    )
  }

The variables and everything works right now but I am getting this error everytime:\

"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Guid' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'user2_gd', line 2, position 15."

Does someone know how to fix this or expierences the same problem? Please reach out.



Read more here: https://stackoverflow.com/questions/64418319/post-2-guid-parameters-to-api-with-angular

Content Attribution

This content was originally published by AntonioSk at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: