-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
35 lines (27 loc) · 790 Bytes
/
encode.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
package api
import (
"io/ioutil"
"net/http"
"github.com/jozsefsallai/watson-as-a-service/parser"
"github.com/jozsefsallai/watson-as-a-service/utils"
)
// EncodeHandler will take the request's body and turn it into a WATSON string.
func EncodeHandler(res http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
var dataType parser.DataType = parser.NewDataType(req.URL.Query().Get("type"))
if len(body) == 0 {
utils.SendJSON(res, 400, true, []byte("Request body is empty!"))
return
}
buffer, err := parser.EncodeAs(body, dataType)
if err != nil {
utils.SendJSON(res, 500, true, []byte(err.Error()))
return
}
utils.SendJSON(res, 200, false, buffer)
}