summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-02-18 18:27:59 +0700
committerShulhan <ms@kilabit.info>2024-02-18 18:27:59 +0700
commite89e0f0857a1d3a6f3079bbecea45a255df431f0 (patch)
treeaace2a707a89af37cee25abb1cedeb6f8b51993c
parent8d27c8b6fa922fbb90bde8ba3675abf535d12422 (diff)
downloadgorankusu-e89e0f0857a1d3a6f3079bbecea45a255df431f0.tar.xz
example: add POST header that can return custom HTTP response code
In some cases we found that if the HTTP response code is 202 the JSON response body is not indented. Turns out this is caused by HTTP Content-Type in response is "text/plain". Closes: https://todo.sr.ht/~shulhan/gorankusu/6
-rw-r--r--example.go27
1 files changed, 22 insertions, 5 deletions
diff --git a/example.go b/example.go
index 60bed1c..4fbf989 100644
--- a/example.go
+++ b/example.go
@@ -10,6 +10,7 @@ import (
"mime/multipart"
"net/http"
"net/url"
+ "strconv"
"sync"
"time"
@@ -27,6 +28,11 @@ const (
pathExampleUpload = `/example/upload`
)
+// List of custom headers.
+const (
+ headerNameXResponseCode = `X-Response-Code`
+)
+
const (
websocketAddress = `127.0.0.1:28240`
)
@@ -283,11 +289,9 @@ func (ex *Example) registerTargetHTTP() (err error) {
Path: pathExample,
RequestType: libhttp.RequestTypeForm,
Headers: KeyFormInput{
- `X-PostForm`: FormInput{
- Label: `X-PostForm`,
+ headerNameXResponseCode: FormInput{
+ Label: headerNameXResponseCode,
Hint: `Custom HTTP header to be send.`,
- Kind: FormInputKindNumber,
- Value: `1`,
},
},
Params: KeyFormInput{
@@ -498,11 +502,24 @@ func (ex *Example) pathExamplePost(epr *libhttp.EndpointRequest) (resb []byte, e
Body: string(epr.RequestBody),
}
+ var (
+ hdrXResponseCode = epr.HttpRequest.Header.Get(headerNameXResponseCode)
+ expRespCode int64
+ )
+ expRespCode, err = strconv.ParseInt(hdrXResponseCode, 10, 64)
+ if err != nil {
+ expRespCode = http.StatusOK
+ }
+
var res = libhttp.EndpointResponse{}
- res.Code = http.StatusOK
+
+ res.Code = int(expRespCode)
res.Message = pathExample
res.Data = data
+ epr.HttpWriter.Header().Set(libhttp.HeaderContentType, epr.Endpoint.ResponseType.String())
+ epr.HttpWriter.WriteHeader(res.Code)
+
return json.Marshal(&res)
}