Go
Scenarios
To use Go to call an API through App authentication, obtain the Go SDK, create a new project, and then call the API by referring to the API calling example.
This section uses IntelliJ IDEA 2018.3.5 as an example.
Prerequisites
- You have obtained the domain name, request URL, and request method of the API as well as the key and secret (or AppKey and AppSecret of the client) of the integration application. For details, see Preparations.
- You have installed Go 1.14 or later. If not, download the Go installation package from the Go official website and install it.
- You have installed IntelliJ IDEA 2018.3.5 or later. If not, download IntelliJ IDEA from the IntelliJ IDEA official website and install it.
- You have installed the Go plug-in on IntelliJ IDEA. If not, install the Go plug-in according to Figure 1.
Obtaining the SDK
Log in to the ROMA Connect console, choose API Connect > API Calling, and download the SDK. The directory structure after the decompression is as follows:
Name |
Description |
---|---|
core\escape.go |
SDK code |
core\signer.go |
|
demo.go |
Sample code |
Creating a Project
- Start IntelliJ IDEA and choose File > New > Project.
On the displayed New Project page, choose Go and click Next.
Figure 2 New Project
- Click ..., select the directory where the SDK is decompressed, and click Finish.
Figure 3 Selecting the SDK directory after decompression
- View the directory structure of the project.
Figure 4 Directory structure of the new project
Modify the parameters in sample code demo.go as required. For details about the sample code, see API Calling Example.
API Calling Example
- Import the Go SDK (signer.go) to the project.
import "apig-sdk/go/core"
- Generate a new signer and enter the key and secret of the integration application.
s := core.Signer{ Key: "4f5f626b-073f-402f-a1e0-e52171c6100c", Secret: "******", }
- Generate a new request, and specify the domain name, method, request URL, query parameters, and body.
r, _ := http.NewRequest("POST", "http://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/api?a=1&b=2", ioutil.NopCloser(bytes.NewBuffer([]byte("foo=bar"))))
- Add the x-stage header to the request to specify an environment name. Add other headers to be signed as necessary.
r.Header.Add("x-stage", "RELEASE")
- Execute the following function to add the X-Sdk-Date and Authorization headers for signing:
s.Sign(r)
- If the subdomain name allocated by the system is used to access the API of HTTPS requests, ignore the certificate verification. Otherwise, skip this step.
client:=&http.Client{ Transport:&http.Transport{ TLSClientConfig:&tls.Config{InsecureSkipVerify:true}, }, }
- Access the API and view the access result.
resp, err := http.DefaultClient.Do(r) body, err := ioutil.ReadAll(resp.Body)
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot