Answer the question
In order to leave comments, you need to log in
How to describe the structure of the input data?
When sending a request, json with data or an error may be returned in response. How to describe the resp.Body structure in this case?
resp, err := http.Get(requestURL)
if err != nil {
// ...
}
defer resp.Body.Close()
Answer the question
In order to leave comments, you need to log in
resp.Body contains only bytes and nothing else.
You can, depending on the status of the resp.Status server response, choose how you process the data.
For example, in my projects it is done like this:
When the request status is 200, I decode it as usual.
If the status is different, I expect it to contain another structure containing the description of the error, and I try to decode it into this structure and return it as an error.
But if error decoding fails, then I simply return an error containing the Body text.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question