Postman, an easy way to parse APIs

Introduction

While preparing this article, I encountered wrong responses from the server. It seemed that the data I was sending to ThingSpeak was wrong. Let’s try to figure out where exactly the problem is.

To do this we will use the program Postman, which allows us to easily make a request to the server and get a response. We can also do this directly in the board code, but with the program it is much easier and faster. This will save us a lot of time. But let’s first see what Postman is.

What is Postman

Postman is a powerful tool for API (Application Programming Interface) development and testing. Here is a summary of some of the key features and benefits of Postman:

  • Easy to use interface:Postman offers a user-friendly and intuitive interface. Its well-structured and easy-to-navigate environment makes API testing and development remarkably more convenient.
  • Creating Requests: allows you to quickly and easily create HTTP requests with different methods (GET, POST, PUT, DELETE, etc.), add parameters, headers and request body.
  • Collections: Postman allows you to organize requests into collections, which is handy for grouping requests by project and sharing them with the team.
  • Automation and Testing: Integrates capabilities to create automated tests using JavaScript, making it easy to verify API responses.
  • Collaboration tools:Postman offers features for team collaboration, including sharing collections, exchanging requests and comments.
  • Generate documentation: you can generate automatic documentation for your APIs that is useful for communicating with other developers.
  • Working with different formats:Postman supports working with different data formats such as JSON and XML, providing the ability to easily convert and process data.
  • Extensibility:With constant updates and addition of new features, Postman provides opportunities for integration with various tools and services.

Overall, Postman is a strong tool that facilitates the process of API development, testing and documentation. Its functionality makes it an indispensable tool for developers, testers, and anyone involved in working with APIs.

Installation

You can install Postman from here. In the installation options, install the “lightweight client” without the need for registration. For this example, we don’t need one. Without registration, the collections feature cannot be used. This is what the installation screen looks like:

When you click on the link, our client opens immediately. But let’s see exactly where the problem is and why we want to use this program.

Problem

We get an error on the line where we try to read the values from the ThingSpeak server. This code should return the last two values of the variable we sent.

String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber +                 
 ".json?api_key=" + writeApiKey + "?results=" + numberOfResults + " HTTP/1.1\r\n"      
  + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n";

If you replace the line of code in the article code, you will get the following answer:

HTTP/1.1 400 Bad Request
Date: Sat, 25 Nov 2023 14:23:22 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 400 Bad Request
Cache-Control: no-cache
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 1800
X-Request-Id: 04f74c1d-f9f5-49ea-bf60-608dc10ed62c
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
X-Frame-Options: SAMEORIGIN
2
-1
0
Closing connection

The “400 Bad Request” error indicates that we are sending something wrong to the server. You can read more about the different HTTP responses here. The error description is – “400 Bad Request – The server cannot or will not process the request due to something that is perceived as a client error (e.g., malformed request syntax, invalid request message formatting, or fraudulent request routing).” 

Let’s use Postman to send the request and see what happens. For this we need the access keys to ThingSpeak. We can get these by logging into the page and going to the channel we want to read from.

Let us now rewrite the query in a form convenient for us:

https://api.thingspeak.com/channels/23*****/fields/1.json?api_key=E5**************&results=2

Let’s run it in Postman

In response we get pretty similar headers and body as the board error.

But let’s take a look at the page of  ThingSpeak. There the correct query is described, which is:

https://api.thingspeak.com/channels/23*****/fields/1.json?api_key=E5**************&results=2

let’s repeat it in Postman:

Now the query returns the correct answer.

Let’s replace the wrong code with the correct one.

String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber +        
 ".json?api_key=" + writeApiKey + "&results=" + numberOfResults + " HTTP/1.1\r\n"   
 + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n";

And let’s get the code on board. If we open the serial console we get:

HTTP/1.1 200 OK
Date: Tue, 28 Nov 2023 20:47:05 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 200 OK
Cache-Control: max-age=7, private
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 1800
X-Request-Id: eac505f1-4a2a-45f6-9ed3-f6f4f6d726ed
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
ETag: W/"47c716b9756448dc96ac69cf9445b49d"
X-Frame-Options: SAMEORIGIN
1c6
{"channel":{"id":23*****,"name":"ThingSpeak","description":"ThingSpeak demo channel","latitude":"0.0","longitude":"0.0","field1":"Internal temp","created_at":"2023-11-18T14:20:41Z","updated_at":"2023-11-18T14:20:41Z","last_entry_id":77},"feeds":[{"created_at":"2023-11-25T14:35:44Z","entry_id":75,"field1":"39.91"},{"created_at":"2023-11-28T20:46:46Z","entry_id":76,"field1":"43.86"},{"created_at":"2023-11-28T20:47:03Z","entry_id":77,"field1":"40.35"}]}
0

Which is the right answer we needed.

Conclusion

As you can see, debugging with Postman is quick and easy. This utility can save you a lot of time in debugging or quickly implementing web queries without uploading the code to a board or emulating it. Numerous articles can be found on the web that describe the process in more detail and show more details about the program. Overall, Postman is another tool that can help us get to the desired result faster.

Happy codding!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *