Baldie47
Baldie47

Reputation: 1274

API returning an object instead of string

I have an API that I set calls with refit, however when calling it I'm receiving an object instead of a string, I don't get what the issue is here.

var userToken = await apiClient.LoginuserBySsnAsync(ssn);

the definition of the API call:

        /// <summary>
        /// Login user by SSN.
        /// </summary>
        /// <param name="ssn">user SSN in 12 digit format.</param>
        /// <returns>user token.</returns>
        [Get("/api/login/loginssn")]
        Task<string> LoginUserBySsnAsync(string ssn);

and in this case the return I'm getting in "userToken" is:

"{\"userToken\":\"rgkjnrekljgtlwgktkwljhnglterkjhnyJuYmYiOjE2NDM2NjAxNDMsImV4cCI6MTY0NjA3OTM0MywiUGF0aWVudHNzbiwerlfkrlkgfnrejgnlrekjglmeMc\"}"

when I execute in the API directly (swagger) the response I get is:

{
  "userToken": "rgkjnrekljgtlwgktkwljhnglterkjhnyJuYmYiOjE2NDM2NjAxNDMsImV4cCI6MTY0NjA3OTM0MywiUGF0aWVudHNzbiwerlfkrlkgfnrejgnlrekjglmeMc"
}

where what I would expect is:

"rgkjnrekljgtlwgktkwljhnglterkjhnyJuYmYiOjE2NDM2NjAxNDMsImV4cCI6MTY0NjA3OTM0MywiUGF0aWVudHNzbiwerlfkrlkgfnrejgnlrekjglmeMc"

I'm not sure what the issue is as I don't have a lot of experience here, but I appreciate any help to improve

Upvotes: 0

Views: 993

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40998

Your API is returning a JSON-serialized object. Probably something like this:

public class UserTokenClass {
    public string UserToken { get; set; }
}

[Get("/api/login/loginssn")]
public async Task<string> LoginUserBySsnAsync(string ssn) {
    
    // Magic

    return JsonConvert.SerializeObject(new UserTokenClass { UserToken = userTokenFromSomewhere });
}

If you want it to return just a plain string, then do that:

return userTokenFromSomewhere;

If you don't have any control over the API code, then deserialize the response in your code:

var token = JsonConvert.DeserializeObject<UserTokenClass>(responseBody);

Upvotes: 2

Related Questions