Perform web requests from Solidity scripts/tests
forge install memester-xyz/surl
- Add this import to your script or test:
import {Surl} from "surl/Surl.sol";
- Add this directive inside of your Contract:
using Surl for *;
- Make your HTTP requests:
// Perform a simple get request
(uint256 status, bytes memory data) = "https://httpbin.org/get".get();
// Perform a get request with headers
string[] memory headers = new string[](2);
headers[0] = "accept: application/json";
headers[1] = "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
(uint256 status, bytes memory data) = "https://httpbin.org/get".get(headers);
// Perform a post request with headers and JSON body
string[] memory headers = new string[](1);
headers[0] = "Content-Type: application/json";
(uint256 status, bytes memory data) = "https://httpbin.org/post".post(headers, '{"foo": "bar"}');
// Perform a put request
(uint256 status, bytes memory data) = "https://httpbin.org/put".put();
// Perform a patch request
(uint256 status, bytes memory data) = "https://httpbin.org/put".patch();
// Perform a delete request (unfortunately 'delete' is a reserved keyword and cannot be used as a function name)
(uint256 status, bytes memory data) = "https://httpbin.org/delete".del();
- You must enable ffi in order to use the library. You can either pass the
--ffi
flag to any forge commands you run (e.g.forge script Script --ffi
), or you can addffi = true
to yourfoundry.toml
file.
- It assumes you are running on a UNIX based machine with
bash
,tail
,sed
,tr
,curl
andcast
installed.
We have example usage for both tests and scripts.
Clone this repo and run:
forge install
Make sure all tests pass, add new ones if needed:
forge test
Forge scripting is becoming more popular. With Solenv your scripts are even more powerful and natural to work with.
- Solenv: Load .env files in Solidity scripts/tests.
- A JSON parser? We found some in-progress work on this front, but nothing quite ready. If you're working on a JSON parser in Solidity, please let us know.
This project uses Foundry. See the book for instructions on how to install and use Foundry.