forked from apimatic/unirest-go
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Response.go
43 lines (36 loc) · 853 Bytes
/
Response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package unirest
import (
"io/ioutil"
"net/http"
)
type Response struct {
Code int
RawBody []byte
Body string
Headers map[string][]string
}
func NewStringResponse(resp *http.Response) (*Response, error) {
res, err := NewBinaryResponse(resp)
if err == nil {
res.Body = string(res.RawBody)
}
return res, err
}
func NewBinaryResponse(resp *http.Response) (*Response, error) {
var res []byte = nil
//read response body
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return makeResponse(resp.StatusCode, res, resp.Header), nil
}
func makeResponse(statusCode int, buff []byte, headers map[string][]string) *Response {
//prepare a new request object
response := new(Response)
response.Code = statusCode
response.RawBody = buff
response.Headers = headers
return response
}