2013-03-30 01:41



I'm fairly new to go.

I use this package to do my json-rpc v 1.0 requests (as golang only implements 2.0)

I have a problem, this server i'm calling return the "id" as a string, like


instead of


The only way I found was to redefine clientResponse using string instead of uint64

type clientResponse struct {
    Result *json.RawMessage `json:"result"`
    Error  interface{}      `json:"error"`
    Id     string           `json:"id"`

and to redefine the exacte same DecodeClientResponse function to use my clientResponse

and instead of CallJson, I call (DecodeClientResponse instead of gjson.DecodeClientResponse):

httprpc.CallRaw(address, method, &params, &reply, "application/json",
            gjson.EncodeClientRequest, DecodeClientResponse)

I find this quite ugly, is there any way to do better ?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douxiji8707 douxiji8707 8年前

    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.


    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) {
        // 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
    点赞 评论 复制链接分享
  • dousiyou1058 dousiyou1058 8年前


    type clientResponse struct {
        Result *json.RawMessage `json:"result"`
        Error  interface{}      `json:"error"`
        # Tell encoding/json that the field is
        # encoded as a json string even though the type is int.
        Id     int           `json:"id,string"`

    As long as the libraries are using encoding/json under the hood this should work.

    点赞 评论 复制链接分享