Chaincode如何调用外部api

时间:2018-06-21 07:40:45

标签: go hyperledger-fabric hyperledger

我想用golang编写的chaincode调用外部api。 下面是我在chaincode中的函数。但是我遇到了问题。

    func (sc *SimpleChaincode) getjson(stub shim.ChaincodeStubInterface) pb.Response {
        url := "http://9.115.193.6:8085"
        ret, err := http.Get(url)

        if err != nil {
            return shim.Error("http get url failed")

        }
        defer ret.Body.Close()

        body, err := ioutil.ReadAll(ret.Body)
        if err != nil {
            return shim.Error("ReadAll ret.Body failed")
        }

        var msg Message
        err = json.Unmarshal(body, &msg)
        if err != nil {
            return shim.Error("unmarshal failed")
        }

        strTime := time.Unix(msg.Time, 0).Format("2006-01-02 15:04:05")
        fmt.Println("Dept:", msg.Dept)
        fmt.Println("Subject:", msg.Subject)
        fmt.Println("Time:", strTime, "\n", msg.Detail)

        logger.Infof("========Dept:====%s===", msg.Dept)
        logger.Infof("========Subject:====%s===", msg.Subject)
        logger.Infof("========Time:====%s===", strTime)
        return shim.Success(getRetReason("getjson", "成功"))
    }

但这是错误的。日志是:

node getjson.js 
Store path:/Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/dataAsset01/dataAsset/hfc-key-store
Successfully loaded user1 from persistence
Assigning transaction_id:  84842351c1987594bff2042dc8d52865efc5d244adada81458c0f6446627a5bc
error: [client-utils.js]: sendPeersProposal - Promise is rejected: Error: 2 UNKNOWN: error executing chaincode: failed to execute transaction: timeout expired while executing transaction
    at new createStatusError (/Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/dataAsset01/dataAsset/node_modules/grpc/src/client.js:64:15)
    at /Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/dataAsset01/dataAsset/node_modules/grpc/src/client.js:583:15
=====results======Error: 2 UNKNOWN: error executing chaincode: failed to execute transaction: timeout expired while executing transaction
Transaction proposal was bad
Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...
Failed to invoke successfully :: Error: Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...

所以我想问一下如何用Chaincode调用外部api。 我写了一个Go服务器来提供rest api:

0 个答案:

没有答案