Help Center > > Developer Guide> Demos> RESTful APIs

RESTful APIs

Updated at: Apr 21, 2020 GMT+08:00

HUAWEI CLOUD BCS provides RESTful APIs to simplify the usage of blockchains. You only need to develop applications that support RESTful APIs to access blockchains without the need to learn Hyperledger Fabric SDKs for Golang, Java, and Node.js. This demo uses a Golang client to show how RESTful APIs are used to invoke a chaincode.

This demo is used only for scenario experience and is not used in actual applications.

Creating a BCS Service

  1. Log in to the BCS console.
  2. Click Buy BCS Service in the upper right corner of the page and set the parameters.

    Parameter

    Setting

    Billing Mode

    Select Pay-per-use.

    Region

    Retain the default value.

    Service Name

    Enter demo.

    Edition

    Select Basic or Professional.

    Blockchain Type

    Select Private.

    Consensus Mechanism

    Select Solo.

    Resource Access Initial Password

    Enter a password.

    Confirm Password

    Enter the password again.

    Advanced Settings

    Select Configure.

    Cluster

    Select Create a new CCE cluster.

    AZ

    Select an AZ.

    ECS Specifications

    Select the flavor of 4 vCPUs | 8 GB.

    ECS Quantity

    Enter 1.

    High Availability

    Select No.

    VPC

    Select Automatically create VPC.

    Subnet

    Select Automatically create subnet.

    ECS Login Method

    Select Password.

    Password of Root User

    If you do not enter a password here, the previously specified resource access initial password will be used.

    Confirm Password

    -

    Blockchain Mgmt. Initial Password

    If you do not enter a password here, the previously specified resource access initial password will be used.

    Confirm Password

    -

    Version

    Select the latest version.

    Volume Type

    Select SFS Turbo.

    Peer Organization

    A peer organization named organization with 2 peers has been automatically created. Retain the default settings.

    NOTE:

    If multiple organizations are involved in the service, you can select Professional for the Edition.

    Orderer Quantity

    Retain the default value 1.

    Enable Data Aging on Orderers

    Select No.

    Security Mechanism

    Only ECDSA can be selected.

    Storage Capacity of Peer Organization

    Retain the default value.

    Ledger Storage

    Select File database (goleveldb).

    Channel Configuration

    The organization organization has been added to the channel automatically. Retain this default setting.

    Enable Support for RESTful APIs

    Select Yes. If you select No, choose More > Enable Support for RESTful APIs in the Operation column on the Service Management page to install RESTful APIs, after the service is created.

    Use EIP

    Select Yes.

    EIP Billed By

    Select Bandwidth.

    EIP Bandwidth

    Set it to 5 Mbit/s.

    Configure Block Generation

    Select No.

    Click Next, confirm the configurations, and click Submit. If the status of the service and organization is Normal, the service has been successfully created.

Installing and Instantiating a Chaincode

  1. Click Manage Blockchain in the Operation column of the BCS service you just created.

  2. On the login page, enter the username, password, and verification code, and click Log In.

    The username is admin, and the password is the Blockchain Mgmt. Initial Password set when you bought the BCS service. If you have not set this password, use the resource access initial password.

  3. Click in the upper left corner of the page.

    The parameters for chaincode installation are as follows.

    Parameter

    Setting

    Chaincode Name

    Enter bcsysq.

    Chaincode Version

    Enter 1.0.

    Ledger Storage

    File database (goleveldb)

    Select All Peers

    Select the checkbox.

    Organization & Peer

    Select peer-0.

    Language

    Select Golang.

    Chaincode File

    Add the downloaded chaincode file chaincode_example02.zip.

    Chaincode Description

    Enter a description of the chaincode.

  4. Click Install to install the chaincode.
  5. After installing the chaincode, click Instantiate in the Operation column of the chaincode list.

    The parameters for chaincode instantiation are as follows.

    Parameter

    Setting

    Channel

    Select channel.

    Chaincode Version

    Select 1.0.

    Initialization Function

    Enter init.

    Chaincode Parameters

    Enter a,200,b,250.

    Endorsement Policy

    Select Endorsement from any of the following organizations.

    Endorsing Organizations

    Select organization.

    Privacy Protection

    Select No.

Configuring the Application

  1. On the Service Management page, download the user certificate of the peer organization organization to the default local path.

  2. Download and decompress the demo project code package fabric-restapi-demo.zip to the local PC, and use an IDE to open it.

    This demo project is a RESTful client compiled using Golang. It enables chaincode invocation through RESTful APIs to achieve chaincode-based money transfer. Use an IDE such as GoLand to open the package. The following figure shows the content of the project.

  3. Decompress the downloaded user certificate to the usercrypto directory of the project, as shown in the following figure.

  4. Modify parameter settings.

    1. Modify the parameters in the conf.yaml file in the config directory as shown and described in the following figure and tables.

    2. Modify the main.go file in the src/restapi directory, as shown in the following figure and tables.

      For each peer that needs to participate in the endorsement, construct an OrgPeer structure including the organization ID and the domain name of the peer. Add the structure to an array of the OrgPeer type, convert the structure into a byte array using the json.Marshal() method, and then convert the structure into a character string. The OrgPeer structure is as follows:

      type OrgPeer struct {

      OrgId string `json:"orgId"`

      PeerDomainName string `json:"peerDomainName"`

      }

      Table 1 Parameters

      Parameter

      Description

      Endpoint

      IP address and port number used to access RESTful APIs. To obtain an IP address, locate the service on the Service Management page, and choose More > View Service Details in the Operation column. The port number is always 32621.

      Path

      Path to the RESTful APIs service. Retain the default value.

      CryptoMethod

      Encryption algorithm. If the ECDSA algorithm is used, set this parameter to SW.

      SignCert

      Path to the signature in the downloaded certificate

      PrvKey

      Private key in the downloaded certificate

      InvokeReq

      Request body parameters. Set these parameters based on the deployed chaincode. The InvokeReq parameter descriptions in the following table are for your reference.

      QueryReq

      Similar to InvokeReq. Set this parameter based on the deployed chaincode.

      Table 2 InvokeReq parameters

      Parameter

      Description

      Example Value

      SignGzipSwitch

      Indication of whether GZIP compression is enabled. 0 indicates disabling, and 1 indicates enabling.

      1

      ChannelId

      Blockchain channel ID

      c123456

      ChaincodeId

      Chaincode ID

      testcode

      ChaincodeVersion

      Chaincode version

      1.0

      UserId

      User ID issued by the organization's CA. The default value for BCS is User1.

      User1

      OrgId

      Organization ID in a blockchain

      1232b2032faafee152b58cd82cecf52e49a22a38

      OrgPeers

      Organization ID and domain name of each peer

      "[{OrgId:" 1232b2032faafee152b58cd82cecf52e49a22a38", PeerDomainName:"peer-1232b2032faafee152b58cd82cecf52e49a22a38-0 .peer-1232b2032faafee152b58cd82cecf52e49a22a38.default.svc.cluster.local "}]"

      Opmethod

      Purpose, that is, to invoke or query chaincodes.

      invoke

      Args

      Chaincode invoking parameters

      ["invoke","a","b","100"]

  5. Build and run main().

    The code will read the QueryReq and InvokeReq parameters in conf.yaml and main.go and call /v1/chaincode/operation of the RESTful APIs to invoke the chaincode. The code running result is as follows.

    This demo uses a simple REST client to invoke the chaincode through RESTful APIs. The returned invocation result is TransactionID encrypted using Base64, and the query result is data encrypted using Base64. The code is for reference only. You can use this project code to understand how to invoke RESTful APIs.

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel