File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import (
12
12
"github.com/gorilla/mux"
13
13
"github.com/speakeasy-api/speakeasy-api-test-service/internal/auth"
14
14
"github.com/speakeasy-api/speakeasy-api-test-service/internal/requestbody"
15
+ "github.com/speakeasy-api/speakeasy-api-test-service/internal/acceptHeaders"
15
16
)
16
17
17
18
func main () {
@@ -27,6 +28,7 @@ func main() {
27
28
r .HandleFunc ("/pagination/cursor" , pagination .HandleCursor ).Methods (http .MethodGet , http .MethodPut )
28
29
r .HandleFunc ("/retries" , retries .HandleRetries ).Methods (http .MethodGet )
29
30
r .HandleFunc ("/errors/{status_code}" , errors .HandleErrors ).Methods (http .MethodGet )
31
+ r .HanldeFunc ("/optional" , acceptHeaders .AcceptHeaders ).Methods (http .MethodGet )
30
32
31
33
log .Println ("Listening on :8080" )
32
34
if err := http .ListenAndServe (":8080" , r ); err != nil {
Original file line number Diff line number Diff line change
1
+ package acceptHeaders
2
+
3
+ import (
4
+ "encoding/json"
5
+ "encoding/xml"
6
+ "net/http"
7
+
8
+ "github.com/speakeasy-api/speakeasy-api-test-service/internal/utils"
9
+ )
10
+
11
+ func HandleAcceptHeaderMultiplexing (w http.ResponseWriter , r * http.Request ) {
12
+ var obj interface {}
13
+ if r .Header ["Accept" ] == "application/json" {
14
+ err := json .Unmarshal ([]byte ("{\" Obj1\" :\" obj1\" }" ), & obj )
15
+ if err != nil {
16
+ utils .HandleError (w , err )
17
+ return
18
+ }
19
+
20
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
21
+
22
+ if err := json .NewEncoder (w ).Encode (obj ); err != nil {
23
+ utils .HandleError (w , err )
24
+ }
25
+ } else if r .Header ["Accept" ] == "application/xml" {
26
+ err := json .Unmarshal ([]byte ("{\" Obj2\" :\" obj2\" }" ), & obj )
27
+ if err != nil {
28
+ utils .HandleError (w , err )
29
+ return
30
+ }
31
+
32
+ w .Header ().Set ("Content-Type" , "application/xml; charset=utf-8" )
33
+
34
+ if err := xml .NewEncoder (w ).Encode (obj ); err != nil {
35
+ utils .HandleError (w , err )
36
+ }
37
+ }
38
+
39
+ }
You can’t perform that action at this time.
0 commit comments