Skip to content

Commit

Permalink
Ensure headers set before writing response data.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Feb 27, 2024
1 parent a10bdb3 commit 2dcbf82
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions services/daemon/rest/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022, 204 Attestant Limited.
// Copyright © 2022, 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -26,16 +26,27 @@ type APIResponse struct {

// sendResponse is a helper to send a JSON response.
func (s *Service) sendResponse(w http.ResponseWriter, statusCode int, resp any) {
if resp == nil {
// No response to send.
w.WriteHeader(statusCode)

return
}

data, err := json.Marshal(resp)
if err != nil {
s.log.Error().Err(err).Msg("Failed to marshal response")
w.WriteHeader(http.StatusInternalServerError)

return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
if resp != nil {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(resp)
if err != nil {
s.log.Error().Err(err).Msg("Failed to marshal response")
}
_, err = w.Write(data)
if err != nil {
s.log.Error().Err(err).Msg("Failed to write response")
}
_, err = w.Write(data)
if err != nil {
s.log.Error().Err(err).Msg("Failed to write response")

return
}
}

0 comments on commit 2dcbf82

Please sign in to comment.