@@ -14,7 +14,7 @@ import (
14
14
"golang.org/x/text/language"
15
15
)
16
16
17
- func jsonrpcError (c * gin.Context , code int , message string , data any , id * float64 ) {
17
+ func jsonrpcError (c * gin.Context , code int , message string , data any , id any ) {
18
18
c .JSON (http .StatusOK , gin.H {
19
19
"jsonrpc" : "2.0" ,
20
20
"error" : gin.H {
@@ -27,6 +27,18 @@ func jsonrpcError(c *gin.Context, code int, message string, data any, id *float6
27
27
c .Abort ()
28
28
}
29
29
30
+ // parseRequestId checks if the JSON-RPC request contains an id field that is either NULL, Number, or String.
31
+ func parseRequestId (data map [string ]any ) (any , bool ) {
32
+ id , ok := data ["id" ]
33
+ _ , isFloat64 := id .(float64 )
34
+ _ , isStr := id .(string )
35
+
36
+ if ok && (id == nil || isFloat64 || isStr ) {
37
+ return id , true
38
+ }
39
+ return nil , false
40
+ }
41
+
30
42
// Controller returns a custom Gin middleware that handles incoming JSON-RPC requests via HTTP. It maps the
31
43
// RPC method name to struct methods on the given api. For example, if the RPC request has the method field
32
44
// set to "namespace_methodName" then the controller will make a call to api.Namespace_methodName with the
@@ -58,7 +70,7 @@ func Controller(api interface{}) gin.HandlerFunc {
58
70
return
59
71
}
60
72
61
- id , ok := data [ "id" ].( float64 )
73
+ id , ok := parseRequestId ( data )
62
74
if ! ok {
63
75
jsonrpcError (c , - 32600 , "Invalid Request" , "No or invalid 'id' in request" , nil )
64
76
return
0 commit comments