-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
responses.go
54 lines (45 loc) · 1.44 KB
/
responses.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
44
45
46
47
48
49
50
51
52
53
54
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
package http
import (
"encoding/json"
"github.com/valyala/fasthttp"
)
func respondWithJSON(ctx *fasthttp.RequestCtx, code int, obj []byte) {
ctx.Response.Header.SetContentType("application/json")
ctx.Response.SetStatusCode(code)
ctx.Response.SetBody(obj)
}
func respondWithETaggedJSON(ctx *fasthttp.RequestCtx, code int, obj []byte, etag string) {
ctx.Response.Header.SetContentType("application/json")
ctx.Response.Header.Set("ETag", etag)
ctx.Response.SetStatusCode(code)
ctx.Response.SetBody(obj)
}
//func respondWithString(ctx *fasthttp.RequestCtx, code int, obj string) {
// ctx.Response.Header.SetContentType("application/json")
// ctx.Response.SetStatusCode(code)
// ctx.Response.SetBodyString(obj)
//}
func respondWithError(ctx *fasthttp.RequestCtx, code int, resp ErrorResponse) {
b, _ := json.Marshal(&resp)
respondWithJSON(ctx, code, b)
}
func respondEmpty(ctx *fasthttp.RequestCtx, code int) {
ctx.Response.SetBody(nil)
ctx.Response.SetStatusCode(code)
}
//func serializeToJSON(obj interface{}) ([]byte, error) {
// buffer := &bytes.Buffer{}
// encoder := json.NewEncoder(buffer)
// encoder.SetEscapeHTML(false)
// err := encoder.Encode(obj)
// if err != nil {
// return nil, err
// }
//
// bytes := buffer.Bytes()
// return bytes, nil
//}