How to get contents of failed HttpRequest?

I am issuing some http requests that can return validation errors when they are not met they are returned with the status code 422. Only when I try to get the body of the http requests for such failures, I keep getting:

!> Fatal exception of type NullReferenceException on thread 8D0B
!> Message: Null Reference Exception for expression: self.Data

The code I am using is:

Http.ExecuteRequest(request) { response in
            if response.Success {
                response.GetContentAsJson() { content in
                    guard let actualResponse = content.Content as? JsonDocument,
                          let responseData = actualResponse.Item("data") as? JsonArray else {
                        completion(.failure(ResultError.invalid("Something went wrong")))
                        return
                    }

                    if let itemResponse = responseData.first as? JsonObject {
                        completion(.success(itemResponse))
                    } else {
                        completion(.failure(ResultError.parseError))
                    }
                }
            } else {
                // 0 appears to mean request failed, e.g. timeout                
                if (response.Code !== 0) {
                    response.GetContentAsJson() { content in
                        guard let actualResponse = content.Content as? JsonDocument else {
                            completion(.failure(ResultError.parseError))
                            return
                        }
    
                        // Something went wrong
                        completion(.failure(ResultError.parseError))
                    }
                } else{
                    completion(.failure(ResultError.networkError))                     // ??
                }
}

I am wondering how I can get the content? Currently, it appears to create a HttpResponse-instance which sets the exception only. Maybe it should also set the response data when it’s a non zero exception code so response.GetContentAsJson can be used? Where can I find the implementation of this code?

It should be here

1 Like

Cool, thank you! I will have a look, first want to solve my Entitlements issue :frowning: