Using Postman to Implement REST Huawei Kits

Erman Derici
Huawei Developers
Published in
6 min readSep 12, 2023
Postman

Hello, everyone 👋
In this article we will explore how you can use Postman to debug and use certain Huawei Kits. I hope to provide you with a general understanding of how the process works. You can apply this understanding to your server to have a workflow with minimal issues.

Please note that you still need to have created a project in AppGallery Connect and have completed the necessary steps. I will be skipping them to keep this as short as possible, but you can find them in the references tab below.

What Is Postman?

Postman is an API platform for building and using APIs. It is a lifesaver for backend development and allows you to easily test/debug your HTTP requests.

Postman User Interface

It has a relatively easy to use interface based on collections and requests. Each HTTP request is a separate file and collections consist of these request files.

File Structure

One of the greatest advantages of Postman is the ability to use both global and collection/local variables. Local variables can only be used in their respective collections while global variables can be used in any collection.

Collection Variables

There is also the authorization method in which you have most of the popular methods used in the industry, such as API Key, JWT Bearer, Bearer Token and OAuth. This is collection wide and all the requests in this collection can choose to inherit their authorization method from the collection or implement their own.

Authorization

Here you can see that this request is using whatever option I have selected in the parent collection.

Request Authorization Inheritance Example

I am going to be following a Health Kit documentation for this example, but the general idea is the same across all kits. Let’s see how we can create a successful HTTP request for Health Kit.

I will open the references document of the Health Kit functionality I wish to use, in my case it is “Querying Sampling Data Details” and check the Protocol, URL and the Data Format required by the server.

Documentation

As we can see, it is an HTTPS POST request, with an URL that has no parameters and requires a JSON format in the request. Let’s create a new request with these in mind 🤠

Creating a New Request

Now we need to add the other required hears and possibly a body. Let’s navigate back to the documentation and see what options we have.

Documentation

As you can see, we do not have any mandatory headers aside from Authorization which I will get to in a second. For body, we have 2 mandatory fields and 1 optional, since it is only 3 fields let’s just write them all 😄

Body

Now we have written all the required headers and body fields except for authorization. We know that it requires a Bearer Token, so we can let Postman handle the header creation and just add our access token in the authorization tab. Or we can use our access token and put a Bearer in front of it as a header. Whichever is more convenient for you 😌

Authorization Option 1
Authorization Option 2

Let’s try sending our request and see if we get any errors.

And it seems like we got a 403 Forbidden error. As disappointing as it is, we still got a response back from the server! Which means it is working, partially 😏 We just need to solve this issue and our request will be fully working!

Sending the Request

Let’s navigate to the result codes page in our documentation and see what the cause of this issue could be.

Result Code

It looks like the first 2 scenarios could be the cause of our issue, as the others do not have any relevancy to what we have done so far. After confirming that our region in AGC is supported by Health Kit, we will work on to solve the issue by navigating to the FAQ as provided by the second item in the list.

FAQ

It seems like our URL was set incorrectly, let’s fix that by following what this FAQ says.

Copy the Location value in the Headers tab of the response and replace our URL with that link.

Response Headers

Trying again, we got a 200 OK Response 😎

Successful Request

You may notice that the response body does not contain any “real data”, that is because there is no data in Health Kit between those time frames 😁

And there you have it; it is really simple to create a functioning HTTP Request using Postman. If you somehow need the code of your request, such as curl or languages such as Kotlin you can navigate to the right side and select code tab. There you can change your desired language.

Code Snippet

Postman Project

We have developed a Postman project that includes all of the REST implementations of Huawei kits which is available to everyone to use as they please. You can export the collection you wish to use to your local machine and run it. I will leave the link in the refences section below, your support is greatly appreciated!

Exporting Collection

Then you can navigate to your own workspace and import the collection

Importing the Collection

Conclusion

In this article, we have briefly explored the basic capabilities and the user interface of Postman. Using Health Kit as an example, we have created an HTTP request, successfully troubleshot to get it working and confirmed that our request was running properly.
These steps apply to all other kits provided by HMS Core and not just Health Kit. As long as you have a project on AppGallery Connect and have finished the integration steps, you can use the related REST functionalities of your integrated kits.

References

--

--