Introduction
The Client Uniform Resource Locator (curl) command line tool sends API requests via URLs and returns responses.
When making requests to Chainstack APIs, it is common to use curl. We recommend the resource Everything curl.
Installing curl:
Your operating system may already include curl, or you may need to download and install it.
You can check the "Get curl" chapter of Everything curl guide for the best way to install curl on your operating system.
curl commands:
Let's take one command as an example:
curl https://nd-111-999-999.p2pify.com/a123c4c24e53815ac933764cb1defa86/ \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_blockNumber","params": [],"id":1}'
The code snippet above represents an API call made to Chainstack's Ethereum mainnet endpoint. The purpose of this call is to request the latest block number using the method
eth_blockNumber
.curl analysis:
Let's step through each line of the code snippet to understand what's happening.
The first line uses the
curl
command to send a request to the URL curl https://nd-111-999-999.p2pify.com/a123c4c24e53815ac933764cb1defa86/
curl command
-X flag
-X POST \
-H flag
The example specifies the
Content-Type
to be application/json
which means the requested resource is a JavaScript Object Notation (JSON) object.-H "Content-Type: application/json" \
-d flag
The
-d
or --data
flag specifies more information sent along with the curl request.In the example, the data object is a list of key value pairs in JSON format. This follows the JSON RPC 2.0 specification which requires the four specific keys seen here.
The
method
, params
, and id
values are modifiable. The jsonrpc
value is required by the specification.-d '{"jsonrpc":"2.0","method":"eth_blockNumber","params": [],"id":1}'
Running curl:
Please enter the curl
code into a terminal window or command line tool on your computer's operating system and click "return."
If the request is successful, curl will return a response object.
{"jsonrpc":"2.0","id":3,"result":"0xe0a763"}
Conclusion
Using curl commands with your node is a handy method to send a request quickly without writing code. That tool also allows to test the node's functionality right from your command line.
Please contact Chainstack Support with any questions by emailing support@chainstack.com or submitting this form.
Comments
0 comments
Article is closed for comments.