{"action":"reopened","issue":{"url":"https://api.github.com/repos/reactiveui/refit/issues/558","repository_url":"https://api.github.com/repos/reactiveui/refit","labels_url":"https://api.github.com/repos/reactiveui/refit/issues/558/labels{/name}","comments_url":"https://api.github.com/repos/reactiveui/refit/issues/558/comments","events_url":"https://api.github.com/repos/reactiveui/refit/issues/558/events","html_url":"https://github.com/reactiveui/refit/issues/558","id":365447689,"node_id":"MDU6SXNzdWUzNjU0NDc2ODk=","number":558,"title":"Content is null when ApiResponse followed by EnsureSucessStatusCodeAsync()","user":{"login":"Dilsy99","id":38723855,"node_id":"MDQ6VXNlcjM4NzIzODU1","avatar_url":"https://avatars1.githubusercontent.com/u/38723855?v=4","gravatar_id":"","url":"https://api.github.com/users/Dilsy99","html_url":"https://github.com/Dilsy99","followers_url":"https://api.github.com/users/Dilsy99/followers","following_url":"https://api.github.com/users/Dilsy99/following{/other_user}","gists_url":"https://api.github.com/users/Dilsy99/gists{/gist_id}","starred_url":"https://api.github.com/users/Dilsy99/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dilsy99/subscriptions","organizations_url":"https://api.github.com/users/Dilsy99/orgs","repos_url":"https://api.github.com/users/Dilsy99/repos","events_url":"https://api.github.com/users/Dilsy99/events{/privacy}","received_events_url":"https://api.github.com/users/Dilsy99/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2018-10-01T12:54:57Z","updated_at":"2018-10-16T12:38:22Z","closed_at":null,"author_association":"NONE","body":"Hi,\r\n\r\nI have noticed an issue with refit when getting a response wrapped in a refit ApiResponse which them calls EnsureSuccessStatusCodeAsync()\r\n\r\nThe ApiException thrown by the EnsureSuccessStatusCodeAsync() has null content even when content exists in the httpResponseMessage.\r\n\r\nThis sort of code:\r\n```cs\r\nTask<ApiResponse<InvestmentsResponseDto>> InsertInvestments(\r\n......\r\n\r\n// ApiResponse<t>\r\nvar investmentsResponse = await this.investmentsHttp.InsertInvestments(\r\n                                                  commandContext.Command.PrivateQuoteId,\r\n                                                  request,\r\n                                                  InvestmentConstants.MediaTypes.InvestmentAccountStrategyV100,\r\n                                                  etagValue);\r\n\r\nawait investmentsResponse.EnsureSuccessStatusCodeAsync();\r\n```\r\n\r\nThe issue seems to be caused because ApiException disposes of the response content \r\n```cs\r\ntry\r\n            {\r\n                exception.ContentHeaders = response.Content.Headers;\r\n                exception.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\r\n                response.Content.Dispose();\r\n            }\r\n```\r\n\r\nIn RequestBuilderImplementation, on error for an ApiResponse response, it created an ApiException which is passed in to the ApiResponse\r\n\r\n```cs\r\nvar exception = await ApiException.Create(rq, restMethod.HttpMethod, resp, restMethod.RefitSettings).ConfigureAwait(false);\r\n\r\n                        if (isApiResponse)\r\n                        {\r\n                            return ApiResponse.Create<T>(resp, default(T), exception);\r\n                        }\r\n```\r\n\r\nThe call to EnsureSuccessStatusCodeAsync() then creates another ApiException (if not successful). However because the response content has been disposed by the previous ApiException creation, the content is now null.\r\n\r\nThere seems to be a couple of ways of resolving this.\r\n\r\n* Remove the dispose calls from ApiException (and ApiResponse?)\r\n* Change EnsureSucessStatusCodeAsync to use the exception passed in via the contructor (if it exists). This sort of code:\r\n```cs\r\nif (!IsSuccessStatusCode)\r\n            {\r\n                if (this.Error == null)\r\n                {\r\n                    this.Error = await ApiException.Create(response.RequestMessage, response.RequestMessage.Method, response).ConfigureAwait(false);\r\n                }\r\n\r\n                Dispose();\r\n\r\n                throw this.Error;\r\n            }\r\n\r\n            return this;\r\n```\r\n\r\nI am happy to make the changes but will be guided by your suggestions for the best way.\r\n\r\nMany thanks.\r\n\r\n### Core 2.1\r\n### Refit  4.6.30\r\n### Visual Studio 2017 v4.7.03056\r\n### Windows 10"}}
{"actor":{"display_login":"onovotny"}}