BREAKING CHANGE: utils package has been removed in favor of specific new packages (http, crypto, strings)
33 lines
803 B
Go
33 lines
803 B
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
func MarshalJSON(w http.ResponseWriter, i interface{}) {
|
|
MarshalJSONWithStatus(w, i, http.StatusOK)
|
|
}
|
|
|
|
func MarshalJSONWithStatus(w http.ResponseWriter, i interface{}, status int) {
|
|
w.Header().Set("content-type", "application/json")
|
|
w.WriteHeader(status)
|
|
err := json.NewEncoder(w).Encode(i)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func ConcatenateJSON(first, second []byte) ([]byte, error) {
|
|
if !bytes.HasSuffix(first, []byte{'}'}) {
|
|
return nil, fmt.Errorf("jws: invalid JSON %s", first)
|
|
}
|
|
if !bytes.HasPrefix(second, []byte{'{'}) {
|
|
return nil, fmt.Errorf("jws: invalid JSON %s", second)
|
|
}
|
|
first[len(first)-1] = ','
|
|
first = append(first, second[1:]...)
|
|
return first, nil
|
|
}
|