Ralph J. Smit Laravel Software Engineer
GitHub Actions is a great tool for every developer. It can be easily incorporated in your development workflow. Sometimes you'd need to visit a certain endpoint, for example in a REST API. Luckily, this is very easy with GitHub Actions. In this tutorial I'll show you how to use curl to visit API endpoints.
First, what is curl? Curl (or cURL) is a tool for getting data from a URL and transferring data to it. It effectively allows you to call a URL from the terminal and display the data returned using one of the HTTP methods.
Skip the explanation and go immediately to the GitHub Actions part.
HTTP methods
If you already know what methods are with RESTful APIs or why this
There are five methods to call a REST API: GET, POST, PUT, PATCH, and DELETE. A method gives information about what someone wants to do with a certain URL. For example, GET means that request information. POST means that you have new information and want to add it.
For example, say you have an API at api.example.com/v1/images
. The same URL can be used to get an image (request) and to add a new image. It would look like this:
-
GET api.example.com/v1/images
– get a certain image -
POST api.example.com/v1/images
– add a certain image
(Most likely you'll also need to provide an image ID or an image URL, but that's not important for understand methods.)
Trying a GET method
Curl can be used from the command line. Let's try it. Run:
curl https://ralphjsmit.com/
If you scroll up, you might see that you've now gotten the HTML of my website homepage. This is also how a browser works.
Making a Curl request
There are many options for curl
, but there is one important one – -X
. This parameter defines the method to use for the request. The syntax looks like this:
curl -X GET https://example.com
With the parameter -X
, you specify the method (e.g. -X POST
or -X PUT
).
curl -X GET https://example.comcurl -X POST https://example.comcurl -X PUT https://example.comcurl -X PATCH https://example.comcurl -X DELETE https://example.com
As you see, with curl you can hit up any API endpoint or webhook!
Using curl with GitHub Actions
It is very easy to use curl. It is already a tool on the command line and GitHub allows you to run CLI commands from GitHub Actions.
A GitHub Actions workflow generally looks something like this:
name: "Hit a webhook" on: push: tags: - '**' jobs: run-updater: runs-on: ubuntu-latest steps: - name: REST API with curl run: | curl -X GET "https://example.com/api/v2/endpoint"
Especially the last part is important: the run
. Here you can add your curl command or your endpoint.
-
First, test your
curl
command on your own terminal or command line interface (CLI). -
Add it to your GitHub Actions.
Wrapping up
As you see, it is ridiculously easy to hit up an API or endpoint with curl in GitHub Actions. It's as simple as running a simple command ánd it can easily be tested! If you have a suggestion to make the article better, feel free to leave a comment below and I'll credit you if I add it.✌️🚀👇
Published by Ralph J. Smit on in Github . Last updated on 28 March 2022 .