Default to json content-type in api
This commit is contained in:
parent
dffbe045e4
commit
4f982bb9cd
@ -120,32 +120,6 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
var rsp []byte
|
var rsp []byte
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
// json codecs
|
|
||||||
case hasCodec(ct, jsonCodecs):
|
|
||||||
var request json.RawMessage
|
|
||||||
// if the extracted payload isn't empty lets use it
|
|
||||||
if len(br) > 0 {
|
|
||||||
request = json.RawMessage(br)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create request/response
|
|
||||||
var response json.RawMessage
|
|
||||||
|
|
||||||
req := c.NewRequest(
|
|
||||||
service.Name,
|
|
||||||
service.Endpoint.Name,
|
|
||||||
&request,
|
|
||||||
client.WithContentType(ct),
|
|
||||||
)
|
|
||||||
|
|
||||||
// make the call
|
|
||||||
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
|
|
||||||
writeError(w, r, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// marshall response
|
|
||||||
rsp, _ = response.MarshalJSON()
|
|
||||||
// proto codecs
|
// proto codecs
|
||||||
case hasCodec(ct, protoCodecs):
|
case hasCodec(ct, protoCodecs):
|
||||||
request := &proto.Message{}
|
request := &proto.Message{}
|
||||||
@ -173,8 +147,36 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// marshall response
|
// marshall response
|
||||||
rsp, _ = response.Marshal()
|
rsp, _ = response.Marshal()
|
||||||
default:
|
default:
|
||||||
http.Error(w, "Unsupported Content-Type", 400)
|
// if json codec is not present set to json
|
||||||
return
|
if !hasCodec(ct, jsonCodecs) {
|
||||||
|
ct = "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
// default to trying json
|
||||||
|
var request json.RawMessage
|
||||||
|
// if the extracted payload isn't empty lets use it
|
||||||
|
if len(br) > 0 {
|
||||||
|
request = json.RawMessage(br)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create request/response
|
||||||
|
var response json.RawMessage
|
||||||
|
|
||||||
|
req := c.NewRequest(
|
||||||
|
service.Name,
|
||||||
|
service.Endpoint.Name,
|
||||||
|
&request,
|
||||||
|
client.WithContentType(ct),
|
||||||
|
)
|
||||||
|
|
||||||
|
// make the call
|
||||||
|
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshall response
|
||||||
|
rsp, _ = response.MarshalJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the response
|
// write the response
|
||||||
|
Loading…
Reference in New Issue
Block a user