/surl

HTTP Library for Solidity based on curl

Primary LanguageSolidityMIT LicenseMIT

surl

Perform web requests from Solidity scripts/tests

Github Actions

Installation

forge install memester-xyz/surl

Usage

  1. Add this import to your script or test:
import {Surl} from "surl/Surl.sol";
  1. Add this directive inside of your Contract:
using Surl for *;
  1. 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();
  1. 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 add ffi = true to your foundry.toml file.

Notes

  • It assumes you are running on a UNIX based machine with bash, tail, sed, tr, curl and cast installed.

Example

We have example usage for both tests and scripts.

Contributing

Clone this repo and run:

forge install

Make sure all tests pass, add new ones if needed:

forge test

Why?

Forge scripting is becoming more popular. With Solenv your scripts are even more powerful and natural to work with.

Goes well 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.

Development

This project uses Foundry. See the book for instructions on how to install and use Foundry.