Accepting nil body values #783
Replies: 2 comments 1 reply
-
@keegancraigcruickshank, this is all expected behavior. Most things expecting a JSON body won't necessarily be happy with an empty body. Clients should not be sending requests without no body -- they will serialize an empty request message to That being said, you could configure a custom codec with the same name ("json") using |
Beta Was this translation helpful? Give feedback.
-
I disagree but am happy to be wrong. I think you raise the crux of my problem. The issue I see is that it EXPECTS a json body. POST does not define a body as required as far as I'm aware and it appears this library makes the assumption it should fail when it's not present. Sending a body of '{}' should be equivalent to not sending a body in my eyes. Thanks for the codec tip, that would have also solved my issue, but I didn't want to rewrite all the serialisation, I just wanted it to treat an empty body as an empty json object in the body. It's worth noting that I only even realised this when I implemented docs for a project I'm working on. You can see the generation by default gives you no payload if you don't set any params. Which should still result in a valid request. https://volleyapp.readme.io/reference/privateremotescoringservice_listscores For anyone coming here, I managed to solve this by doing the following: package main
func linkAPIServices(mux *http.ServeMux, config config.Config) {
privateTeamsPattern, privateTeamsHandle := teamsv1privateconnect.NewPrivateTeamsServiceHandler(privateTeamsService)
mux.Handle(privateTeamsPattern, privateTeamsHandle)
}
func normalizeBody(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Body == nil || r.ContentLength == 0 {
// Create a new reader with an empty JSON object
emptyBody := bytes.NewReader([]byte("{}"))
r.Body = io.NopCloser(emptyBody)
r.ContentLength = 2 // Length of "{}"
} else {
// Read the entire body
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
// Close the original body
r.Body.Close()
// If the body is empty (e.g., ""), replace it with "{}"
if len(body) == 0 {
body = []byte("{}")
}
// Create a new reader with the body content
r.Body = io.NopCloser(bytes.NewReader(body))
r.ContentLength = int64(len(body))
}
// Ensure the content type is set to application/json
r.Header.Set("Content-Type", "application/json")
next.ServeHTTP(w, r)
}
}
func main() {
appConfig := config.Init()
mux := http.NewServeMux()
linkAPIServices(mux, appConfig)
wrappedMux := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
normalizeBody(mux.ServeHTTP).ServeHTTP(w, r)
})
_= http.ListenAndServe(
h2c.NewHandler(wrappedMux, &http2.Server{}),
)
} |
Beta Was this translation helpful? Give feedback.
-
Hey all, hopefully someone can assist here.
When calling my service (go) I get:
This only happens when no body is send with the request. For example:
The second I add a data parameter, for example:
Even if it's an empty JSON object, it serializes and works correctly. Is there any way I can simply normalize an empty payload to be an empty message, to ensure this works correctly?
A simplified version of my main for reference:
Beta Was this translation helpful? Give feedback.
All reactions