Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Type Plain Text Deserialize Response #170

Open
stephenstroud opened this issue Nov 11, 2020 · 4 comments
Open

Content-Type Plain Text Deserialize Response #170

stephenstroud opened this issue Nov 11, 2020 · 4 comments

Comments

@stephenstroud
Copy link

I'm trying to deserialise a Text/Plain response, however, found the JSON is the default. I've created the following but doesn't feel right, Isn't there an easier way? Moreover, it would be great in addition to JsonResponseDeserializer if there were more pre-made deserializer classes to pick from.


      public override T Deserialize<T>(string content, HttpResponseMessage response, ResponseDeserializerInfo info)
        {   
            if(typeof(string).IsAssignableFrom(typeof(T)))
            {
                return (T)(object)content;
            }

            throw new InvalidDataException("Plain Text response deserializer can only be used with string");
        }

@canton7
Copy link
Owner

canton7 commented Nov 11, 2020

Can you add more detail on what you're trying to achieve, please?

Using an interface method which returns Task<string> will always return the raw response, without going through a deserializer.

@stephenstroud
Copy link
Author

stephenstroud commented Nov 11, 2020

Instead I have Task<Response<string>> decorated with [AllowAnyStatusCode] As I need granular control over the response as I need to act on different Status Codes.

i.e.

  if (response.ResponseMessage.StatusCode == HttpStatusCode.Unauthorized)...

My proposed solution has worked above but good to know if there is another way.

Task<Response<string>> Does fail using the default JSON deserializer

@canton7
Copy link
Owner

canton7 commented Nov 11, 2020

Can you use Response<string>.StringContent?

The fact that Response<string>.GetContent() uses the deserializer is an oversight, but I'm not sure whether I can change it safely at this point.

@canton7
Copy link
Owner

canton7 commented Dec 3, 2020

I think this is probably a candidate for 2.0 -- I want to rethink handling of strings there entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants