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. The tests also demonstrate how surl can be used to request quotes from DEX aggregators and parse their json response with cheatcodes.
Clone this repo and run:
forge install
Get a 1inch API Key and set it in a .env
file (copy .env.example
).
Make sure all tests pass, add new ones if needed:
forge test
Forge scripting is becoming more popular. With Surl you can extend your scripts easily with HTTP requests.
This project uses Foundry. See the book for instructions on how to install and use Foundry.