Most developers use VS Code as development tool. The REST Client is a popular VS Code extension to help developers send and receive HTTP requests/responses with ease. It is exceptionally useful in testing API calls for different protocols supported by Chainstack. Before you begin, ensure you have the following:
- A Chainstack account (with a deployed node);
- Visual Studio Code installed
Installation
- Open VS code, click on extension logo in the left panel.
- Search for rest-client in search bar and select it from the list.
- In the page opened, click Install.
- Restart VS Code, the extension should be ready to use.
How to use it
Open VS Code and create a new HTTP file. Type in:
chainstack.com
A small line of text Send Request appear on top of the line, it means the extension is successfully installed.
There are four ways to send an HTTP request.
- Click the Send Request link above the request (this will appear if the file's language mode is HTTP).
- Use Ctrl+Alt+R (Cmd+Alt+R for macOS).
- Right-click and then select Send Request.
- Press F1 and then select/type Rest Client: Send Request.
If the request is valid, a response is returned. Text in the response is formatted and colour coded for readability.
Using it with Chainstack endpoint
A typical Chainstack blockchain endpoint is in the format of:
https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d
Using Ethereum mainnet elastic node as example, paste the code into VS Code. (Change the endpoint address.)
GET https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d HTTP/1.1
content-type: application/json
{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 0
}
This should return:
Using it with GraphQL endpoint
Copy the code to see it in action. The authorization field and URL needs to be changed.
POST https://nd-123-456-789.p2pify.com//graphql
Content-Type: application/json
Authorization: Basic username:password
X-REQUEST-TYPE: GraphQL
query{
block{
number
hash
}
}
The response:
Sample code with variables and delimiter
Another useful feature in REST Client is the delimiter. In VS Code, different requests can be store in the same file and separated with ###.
REST Client supports variables too. Variables, file variable to be more specific because REST Client supports 3 types of variables, are declared with @ sign and reference with {{}}. Copy the code below to see it in action.
###
@url = https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d
@contentType = application/json
###
GET {{url}} HTTP/1.1
content-type: {{contentType}}
{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 0
}
###
GET {{url}} HTTP/1.1
content-type: {{contentType}}
{
"jsonrpc": "2.0",
"method": "eth_syncing",
"params": [],
"id": 0
}
###
Comments
0 comments
Article is closed for comments.