-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
41 lines (33 loc) · 853 Bytes
/
decode.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
package api
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/genkami/watson"
"github.com/jozsefsallai/watson-as-a-service/utils"
)
// DecodeHandler will take the request's body (WATSON string) and decode it.
func DecodeHandler(res http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
utils.SendJSON(res, 500, true, []byte(err.Error()))
return
}
if len(body) == 0 {
utils.SendJSON(res, 400, true, []byte("Request body is empty!"))
return
}
var buffer interface{}
err = watson.Unmarshal(body, &buffer)
if err != nil {
utils.SendJSON(res, 500, true, []byte(err.Error()))
return
}
output, err := json.Marshal(&buffer)
if err != nil {
utils.SendJSON(res, 500, true, []byte(err.Error()))
return
}
utils.SendJSON(res, 200, false, output)
}