Sometimes I run into the need to build a quick test service, like this.

Build an HTTP Service, which can be run locally, but also needs to be accessible on the public network, and request the service to get a custom set of JSON data. For no other reason than to be able to use it for testing quickly.

At this time I want to complete in the shortest possible time, such as a minute to write it, how can I do this time?

For example, you might think of running Flask or FastAPI, changing the sample code, and then running it with a single Python command.

For example, the code looks like this.

1
2
3
4
5
6
7
8
from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

Then I ran it with the command.

1
uvicorn main:app --reload

OK, the promised custom JSON has been completed.

But I need to add requirements, I need to support cross-domain access, how do I do that? At this point I may have to search for the FastAPI cors keyword again, and then find the https://fastapi.tiangolo.com/tutorial/cors/ documentation, and then add some configuration like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
...

It’s okay, right?

So now I’ve changed my requirements. What if I want to return an image? What if I want to return a file? What if I want HTTPS access?

Or even say, I am not familiar with the code written what to do? In order to get this API Service I have to spend most of an hour, too much to lose it.

After all, everyone is quite busy.

So I would think, these simple things, why write code to solve ah? Is there no tool to do it through some visual configuration?

If you also have this trouble, please continue to look down.

Solutions

So now my need is: I want to quickly build an API Server with a convenient tool that can be configured to return JSON or images or files and so on, or even say dynamic routing, dynamic forwarding and so on, and it would be nice if these steps could also be done through a visual graphical interface.

Here we go, today we recommend a tool called Mockoon.

Mockoon is a tool that can help us quickly build API services through a graphical interface, supporting data simulation, route resolution, cross-domain access, HTTPS, custom delay, Docker, and all kinds of features you want, while supporting Windows, Mac, Linux, the overall page looks like this.

The layout is similar to PostMan.

For example, on the left side we can configure a list of requests, and on the right side we can configure details such as whether it is a GET or POST request, what the path is, what the Response Body is, what the Response Headers are, and also some rules and basic settings.

In addition, at the top I can configure the running host and port, and then there is a run button in the upper left corner, a click is equivalent to start the Server, after starting the button will turn red, and then press it to stop, for example, here I have configured to run on the local 3894 port.

Then I modified Body.

1
2
3
4
5
6
7
{
  "data": [
    {"id": 1, "name": "Picture3", "url": "https://qiniu.cuiqingcai.com/l4ol8.jpg"},
    {"id": 2, "name": "Picture2", "url": "https://qiniu.cuiqingcai.com/zy2w3.jpg"},
    {"id": 3, "name": "Picture1", "url": "https://qiniu.cuiqingcai.com/v10oo.jpg"}
  ]
}

Here I return a list in JSON format, containing three fields.

Then I’m going to configure cross-domain access by adding a Response Header.

1
Access-Control-Allow-Origin: '*'

Then click the Run button in the upper-left corner and you are done.

Mockoon also provides a shortcut access, then tap the Open button in the upper right corner.

The browser then opens and you can see the data.

In this way, we have completed the construction of the API Server through a very simple visual configuration, if skilled a minute to complete.

In addition, there are too many functions, such as HTTPS, multi-request processing, logging, routing, template configuration here is not to describe one by one, when used to check the documentation on it!

In addition, Mockoon also supports command line, for example, you can quickly create an API Server by using mockoon-cli, as shown in the figure.

Command line usage and installation can be found at: https://github.com/mockoon/cli#installation

The above is a brief introduction to this tool, more features waiting for your exploration!


Reference https://cuiqingcai.com/30009.html