The json-rpc v 1.0 specifies that:
id - The request id. This can be of any type. It is used to match the response with the request that it is replying to.
That is, id
can be anything (even an array), and the server response should contain the same value and type for id, which in your case it doesn't do. So, the server you communicate with is not doing its job correctly, and is not following the json-rpc v 1.0 specs.
So, yes, you need to do the "ugly" solution and create a new decoder function for this 'broken' server. Jeremy Wall's suggestion works (but int
should be changed to uint64
) and should at least make you avoid using string
as type.
Edit
I don't know the httprpc
package enough to know how it handles the Id
value. But if you want either string or int, you should be able to set Id in clientResponse
to:
Id interface{} `json:"id"`
When checking the value in Id
you use a type switch:
var id int
// response is of type clientResponse
switch t := response.Id.(type) {
default:
// Error. Bad type
case string:
var err error
id, err = strconv.Atoi(t)
if err != nil {
// Error. Not possible to convert string to int
}
case int:
id = t
}
// id now contains your value