Mock a REST API with Pipedream
@dylburger
code:
data:privatelast updated:3 years ago
@dylburger/
Mock a REST API with Pipedream

How this works

Pipedream lets you create a free HTTP endpoint you can send any HTTP requests to in one click. You can run any Node.js code or pre-built actions for each of these requests. We call your code a workflow.

You don't need to manage a server or any infrastructure to receive these HTTP requests or run this code. Pipedream manages that so you focus on how your workflow works.

This workflow creates an API mocking service you can use to observe how POST, GET, DELETE, and other HTTP methods work.

Usage

Once you click the Fork button to create a copy of this workflow in your own account, we'll create an HTTP endpoint URL you can send any request to. You'll see this in the HTTP trigger section at the top of your workflow.

Storing data with a POST request

POST data to a collection like /names to create a new resource at /names/1. For example, send an HTTP request like below, and we'll respond with the id and key where you can retrieve the resource:

$ curl -X POST -d '{ "name": "Luke"}' -H 'Content-Type: application/json' https://yourendpoint.m.pipedream.net/names
{"id":1,"key":"/names/1"}

Retrieving existing data with a GET request

You can retrieve data at a given resource by making an HTTP GET request to that path. For example, let's fetch the data we POSTed above:

$ curl https://yourendpoint.m.pipedream.net/names/1
{"name":"Luke"}

This is not standard REST, but you can list all resources, under all collections, by making a request to the / resource:

$ curl https://yourendpoint.m.pipedream.net/
["/names/1", "/cars/1", "/cars/2"]

Making a request to a specific collection lists all the resources under that collection:

$ curl https://yourendpoint.m.pipedream.net/cars
["/cars/1", "/cars/2"]

Making a GET request to a non-existent key should return a 404 Not Found status.

Modifying a resource with a PUT or PATCH request

If you want to modify an existing resource, you can do that with a PUT or a PATCH request. For example, to change the name of the /names/1 resource we created above, run

$ curl -X PUT -d '{ "name": "Leia"}' -H 'Content-Type: application/json' https://yourendpoint.m.pipedream.net/names/1
{"message":"Updated /names/1"}

$ curl https://yourendpoint.m.pipedream.net/names/1
{"name":"Leia"}

Right now, PUT and PATCH are synonymous. You can only overwrite the whole value of a resource in this specific API.

Deleting a key using a DELETE request

curl -X DELETE https://yourendpoint.m.pipedream.net/testKey

This will delete the data at testKey. Subsequent GET requests at this key will return a 404 Not Found status code.

To completely reset the data you've stored in your REST API, issue a DELETE request to the / resource:

$ curl -X DELETE https://yourendpoint.m.pipedream.net/
{"message":"Deleted all resources, API reset"}

This feature is also not standard in RESTful APIs, but provides a nice way to quickly delete all data via the API itself.