Testing chaincode from REST endpoints

OBP provides a REST proxy to connect chaincode via REST endpoints. Whichever chaincode you want to be executed via REST services should be configured on the respective REST proxy. This configuration can be seen in the Chaincode deployment section in this chapter. In this section, we see how to invoke the REST endpoints, how to connect to the required functions, and how to pass the parameters.

There are two REST endpoints available:

  • Query: To execute any function to query data from the ledger:

Syntax: <host name>:<port>/<restproxy>/bcsgw/rest/v1/transaction/query

  • Invocation: To execute any function to save data to the ledger or query data from the ledger. The query could also be done from this endpoint, however, the execution would be slow to fetch and return the data. So, it is recommended to use the query endpoint in cases where you are fetching data from the ledger:

Syntax: <host name>:<port>/<restproxy>/bcsgw/rest/v1/transaction/invocation

For both these endpoints, the request input is the same, which is a JSON request and the following is the typical structure of a JSON request:

{
"channel":<channel name>,
"chaincode":<chaincode name>,
"method": <method name>,
"args":[<arguments separated by comma>]
}

Multiple chaincodes can be configured in a single REST proxy. So, the channel and chaincode parameters in the input JSON help to dispatch the request to the corresponding chaincode. Both the endpoints are POST calls. Two headers, Authorization and Content-Type, should be passed in every call.

We are using the OBP SDK, which has a default username and password: [email protected]/ and Welcome1.

The header should look as follows:

  • Authorization : Basic Y3VzdG9tZXJ0ZW5hbnRAb3JhY2xlLmNvbTpXZWxjb21lMSA=
  • Content-Type: application/json
  • Target endpoint: https://<host name>:<port>/<restproxy>/bcsgw/rest/v1/transaction/<invocation or query>

These are references to test the earlier chaincode from Postman (you can use any REST client here for testing). The following are target endpoint invocation input -

  • Target endpoint – invocation to insert receiver:
    • Target endpoint: /invocation
    • Target method: insertReceiver
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"insertReceiver","args":["std123", "Anand Yerrapati", "Blockchain"]}

  • Target endpoint—query to query by receiver ID:
    • Target endpoint: /query
    • Target method: queryReceiverById
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"queryReceiverById","args":["std123"]}

  • Target endpoint – invocation to insert certificate:
    • Target endpoint: /invocation
    • Target method: insertCertificate
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"insertCertificate","args":["cert1234","1234","ORU Blockchain Certificate","std123","ORU","IT","6/5/2019","","Blockchain Course Completed","","","Issued"]}

  • Target endpoint – invocation to query certificate by ID:
    • Target endpoint: /query
    • Target method: queryCertificateById
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"queryCertificateById","args":["cert1234"]}

  • Target endpoint – invocation to approve certificate:
    • Target Endpoint: /invocation
    • Target method: approveCertificate
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"approveCertificate","args":["cert1234","Approved","6/5/2019 05:04:45 PM"]}

  • Target endpoint – invocation to query certificate by ID:
    • Target endpoint: /query
    • Target method: queryCertificateById
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"queryCertificateById","args":["cert1234"]}

  • Target endpoint – invocation to query all certificates:
    • Target endpoint: /query
    • Target method: queryAllCertificates
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"queryAllCertificates","args":[]}

  • Target endpoint – invocation to query endpoint to get record history:
    • Target endpoint: /query
    • Target method: getRecordHistory
    • Input JSON: {"channel":"channeleducation","chaincode":"cceducation","method":"getRecordHistory","args":["cert1234"]}

The following is the response of getRecordHistory:

Response message
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset