Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: support additional content type #903

Merged
merged 6 commits into from
Feb 2, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use Request.FormValue
  • Loading branch information
oncilla committed Jan 25, 2021
commit 870ced497b9258ec9fb2c5f7ca8da929a586f8b1
22 changes: 6 additions & 16 deletions http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -78,7 +76,7 @@ func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
enc.Encode(payload{Level: lvl.Level()})
case http.MethodPut:
requestedLvl, err := decodePutRequest(r.Header.Get("Content-Type"), r.Body)
requestedLvl, err := decodePutRequest(r.Header.Get("Content-Type"), r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
enc.Encode(errorResponse{Error: err.Error()})
Expand All @@ -95,23 +93,15 @@ func (lvl AtomicLevel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// Decodes incoming PUT requests and returns the requested logging level.
func decodePutRequest(contentType string, body io.Reader) (zapcore.Level, error) {
func decodePutRequest(contentType string, r *http.Request) (zapcore.Level, error) {
if contentType == "application/x-www-form-urlencoded" {
return decodePutURL(body)
return decodePutURL(r)
}
return decodePutJSON(body)
return decodePutJSON(r.Body)
}

func decodePutURL(body io.Reader) (zapcore.Level, error) {
pld, err := ioutil.ReadAll(body)
if err != nil {
return 0, err
}
values, err := url.ParseQuery(string(pld))
if err != nil {
return 0, err
}
lvl := values.Get("level")
func decodePutURL(r *http.Request) (zapcore.Level, error) {
lvl := r.FormValue("level")
if lvl == "" {
return 0, fmt.Errorf("must specify logging level")
}
Expand Down