From bd274db7a798526921cd15ecf32887e13cb6aaf9 Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Mon, 1 Oct 2018 09:07:41 +0800 Subject: [PATCH] Add application/json to response header --- client/rpc/block.go | 2 ++ client/rpc/status.go | 2 +- client/rpc/validators.go | 4 +-- client/tx/broadcast.go | 46 ++++++++++----------------------- client/tx/query.go | 2 +- client/tx/search.go | 2 +- client/utils/rest.go | 2 +- x/auth/client/rest/query.go | 4 +-- x/auth/client/rest/sign.go | 1 + x/bank/client/rest/broadcast.go | 1 + 10 files changed, 26 insertions(+), 40 deletions(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index e1792e03a..e5873152e 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -133,6 +133,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { w.Write([]byte(err.Error())) return } + w.Header().Set("Content-Type", "application/json") w.Write(output) } } @@ -152,6 +153,7 @@ func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { w.Write([]byte(err.Error())) return } + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/client/rpc/status.go b/client/rpc/status.go index 80290ab48..596870bf3 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -72,7 +72,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { w.Write([]byte(err.Error())) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/client/rpc/validators.go b/client/rpc/validators.go index e3b5e29a4..f0c04e75e 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -158,7 +158,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { w.Write([]byte(err.Error())) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } } @@ -179,7 +179,7 @@ func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerF w.Write([]byte(err.Error())) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/client/tx/broadcast.go b/client/tx/broadcast.go index 286c26473..42fe64dee 100644 --- a/client/tx/broadcast.go +++ b/client/tx/broadcast.go @@ -39,46 +39,28 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } - var output []byte + var res interface{} switch m.Return { case flagBlock: - res, err := cliCtx.BroadcastTx(m.TxBytes) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - output, err = cdc.MarshalJSON(res) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } + res, err = cliCtx.BroadcastTx(m.TxBytes) case flagSync: - res, err := cliCtx.BroadcastTxSync(m.TxBytes) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - output, err = cdc.MarshalJSON(res) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } + res, err = cliCtx.BroadcastTxSync(m.TxBytes) case flagAsync: - res, err := cliCtx.BroadcastTxAsync(m.TxBytes) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - output, err = cdc.MarshalJSON(res) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } + res, err = cliCtx.BroadcastTxAsync(m.TxBytes) default: utils.WriteErrorResponse(w, http.StatusInternalServerError, "unsupported return type. supported types: block, sync, async") return } - + if err != nil { + utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + output, err := cdc.MarshalJSONIndent(res, "", " ") + if err != nil { + utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/client/tx/query.go b/client/tx/query.go index f1d13b608..9999d663a 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -142,7 +142,7 @@ func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.H w.Write([]byte(err.Error())) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/client/tx/search.go b/client/tx/search.go index 57b50d684..11e84f419 100644 --- a/client/tx/search.go +++ b/client/tx/search.go @@ -180,7 +180,7 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http. w.Write([]byte(err.Error())) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/client/utils/rest.go b/client/utils/rest.go index 5e3deb71d..52c253781 100644 --- a/client/utils/rest.go +++ b/client/utils/rest.go @@ -245,6 +245,6 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index 7321c553b..c03d47cba 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -112,12 +112,12 @@ func QueryBalancesRequestHandlerFn( } // print out whole account - output, err := cdc.MarshalJSON(account.GetCoins()) + output, err := cdc.MarshalJSONIndent(account.GetCoins(), "", " ") if err != nil { utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error())) return } - + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/x/auth/client/rest/sign.go b/x/auth/client/rest/sign.go index 6894d1c5d..13da2fd4d 100644 --- a/x/auth/client/rest/sign.go +++ b/x/auth/client/rest/sign.go @@ -57,6 +57,7 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } + w.Header().Set("Content-Type", "application/json") w.Write(output) } } diff --git a/x/bank/client/rest/broadcast.go b/x/bank/client/rest/broadcast.go index 6e34acdaa..5d9ffa71e 100644 --- a/x/bank/client/rest/broadcast.go +++ b/x/bank/client/rest/broadcast.go @@ -38,6 +38,7 @@ func BroadcastTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) ht utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } + w.Header().Set("Content-Type", "application/json") w.Write(output) } }