HttpClient method returning a stream

I have created a class with a method to send httpclient requests to an api. The method returns the result as a stream. I return a stream because I need to deserialise the stream multiple times, and with different objects depending on the api.

I feel that it might be incorrect to return a stream. Would it be better if I returned a class with a stream property and a dispose method that disposes the stream? I then wouldn't require a using statement to dispose the stream?

Thanks.

Edit:

The method takes in the http method, url, content and headers and returns a response stream. I then need to deserialise to check for the error status, and if there are no errors I stream into the full response object. Unfortunately, the api returns a 200 even with errors so I have to check for errors first.

Depending on the scenario, I have to deserialse to different response objects.

Posted Nov 11, 2021, 2:28 AM UTC · edited

726
wazzamatazz•

I appreciate that you might not have control over this, but if the API response contains errors, it should ideally be returning an HTTP status code that indicates that the call was unsuccessful and/or setting the content type on the response to indicate that it contains error information (such as an RFC 7807 problem details object) rather than the expected response content.

11
rawpineappleOP•

I agree. I've recently had to call apis from two different companies and they both return a 200 on error. It's a bit frustrating :).

5
wazzamatazz•

You have my sympathies! If the responses are JSON, you could always try deserializing the response as a JsonElement, examining it to see if it contains the properties that indicate it is an error response, and then either deserializing the JsonElement to the actual type you were expecting or extracting and throwing the error information?

EDIT

Here's an example of what I mean:

async Task<T?> ParseResponseAsync<T>(HttpResponseMessage response, JsonSerializerOptions? options = null) {
    var json = await response.Content.ReadFromJsonAsync<JsonElement>(options);
    if (json.TryGetProperty("error", out var prop)) {
        // Error occurred; extract error information from prop.
        throw new Exception(prop.GetString());
    }
    
    return json.Deserialize<T>(options);
}
5
rawpineappleOP•

Thanks wazzamatazz.

2
OneThatWalks•

Maybe I am not understanding this correctly, but couldn’t you read the stream into a string and return that? The caller and other things can deserialize the string however they want.

2
rawpineappleOP•

I want to deserialise the stream multiple times like this:

JsonSerializer.DeserializeAsync<BaseStatusResponse>(response);

1
OneThatWalks•

Newtonsoft and System.Text both support deserialization from string parameters.

https://www.newtonsoft.com/json/help/html/deserializeobject.htm

Storing a string removes the need to manage the disposal of the stream somewhere else that may be uncomfortable. It also can be used with any serialization library.

1
rawpineappleOP•

Ok, that sounds good. Thanks.

2
Alikont•

For Newtonsoft you can first deserialize it to JObject. This will do all the parsing, but not conversion to your object.

Then you can convert JObject to any object multiple times.

1
rawpineappleOP•

Thanks Alikont.

1
CyTrain•

Why are you deserializing multiple times? Are you looking for something like Generic Classes? Or have I misunderstood?

2
rawpineappleOP•

I deserilize multiple times because I need to check for errors in the response before deserializing into full response object. I could probably check for errors in a response string, but decided to deserilize it.
After deserializing twice, I use the stream again to save as a blob.

0
CyTrain•

But why? Just deserialize once, like this:

(Change fields to properties, I left them as fields for brevity)

public class ApiResponse
{
    public int Status;
    public string ErrorInfo;
}
public class ApiResponse<T> : ApiResponse
{
    // only needs to be nullable if using NRT
    public T? Response;
}
public class ResponseA
{
    public string SomeInfoA;
    public DateTime DateInfoA;
}

And then your actual method:

public async Task<ApiResponse<ResponseA>> GetInfoA()
{
    var resp = await myHttpClient.GetAsync("https://...");
    var respStr = await resp.ReadAsStringAsync();
    var respObj = JsonSerializer.Deserialize<ApiResponse<ResponseA>>(respStr);
    // error checking goes here
    return respObj;
}
2
rawpineappleOP•

If there are errors, then the ResponseA json will not exist. Will this method handle that?

Thanks for your help.

1
CyTrain•

It can - that should be part of your error handling (checking if ApiResponse<T>.Reponse is null) - it will be null if the property does not exist or is null in the JSON.

By the way - make it public T? Response instead of public T Response if you're using nullable reference types.

You could also combine ApiResponse and ApiResponse<T> to just ApiResponse<T> but I like to leave them separate in case I need one explicitly without a response type, e.g. throwing an exception.

2
quentech•

I have created a class with a method to send httpclient requests to an api. The method returns the result as a stream. I return a stream because I need to deserialise the stream multiple times.. I deserilize multiple times because I need to check for errors in the response before deserializing into full response object

What is the point of this class you have created if not to encapsulate the details of the api returning 200 on error and needing to inspect the response before you know what class to deserialize?

The methods in the class should not return streams.

It's also perhaps worth knowing that you cannot read a http response stream multiple times. I guess I'm not totally certain off-hand as you seem to say it works, but I would expect an attempt to deserialize the stream a second time would throw an exception on any attempt to seek to the beginning of the stream. If it is working, HttpClient must be internally copying the response stream to a memory stream.

2
rawpineappleOP•

I want to use this method for a number of different apis. Some might return a 400 when an error occurs and I will then handle the error in my method. If it returns a 200 when an error occurs, then I want to check the stream for errors. If there are no errors then I will deserialise to the object for that api.

I am setting the stream position to 0 after deserializing the stream.

1
cryo•

I am setting the stream position to 0 after deserializing the stream.

That doesn't help. These streams are likely not fully buffering and can't be safely consumed more than once.

3
rawpineappleOP•

Ok, but it seems to work without issue.

1
Laurent9999•

Not related to your issue directly but you might want to take a look at Refit, a .NET foundation library that helps a lot in querying Apis: you define an interface where each method is an api call (you can use attributes to specify Http method, query strings, etc) and Refit will generate an implementation for it that you can use in your app. It handles a lot of tricky things like query strings HttpClient doesn’t.

2
rawpineappleOP•

Thanks. I'll take a look.

2
[deleted]•

[deleted]

1