This is the 9th day of my participation in the August Wen Challenge.More challenges in August

The postman tool was used to test the JSON interface used to interact with the background. Later, it was found that the curl command can also send POST or GET requests. Moreover, the curl command can be installed on both Linux and Windows systems, making it easy to test. This paper uses KOA to create a Web server, provides several urls, and then uses curl command to test.

1, the source

The complete source code is as follows, assuming the source file name is koa_test.js:

Curl curl curl curl curl curl curl curl curl curl curl And all json fields are in parentheses. */ const koa_router = require("koa-router"); const Koa = require("koa"); const koa_bodyparser = require("koa-bodyparser"); const log = require('.. /lib/log.js') const router = koa_router(); const g_port = 4000; Curl http://127.0.0.1:4000/ / * * / router. Get ('/', async (CTX) = > {CTX. Type = 'HTML'; ctx.body = '\r\nwelcome to koa'; }); router.get("/status",async (ctx) => { const now = new Date(); ctx.body = `api-service lives! (${now})`; }); router.get('/about', async (ctx) => { ctx.type = 'html'; ctx.body = '<a href="/">About Page</a>'; }); router.get('/about/foobar', async (ctx) => { ctx.type = 'html'; ctx.body = 'here is /about/foobar'; }); router.post("/set", async (ctx) => { log.print('got: ', ctx.request.body); Var res = new Object(); res['ret'] = 0; ctx.body = res; // return json, normal}); function main() { var app = new Koa(); app.use(koa_bodyparser({ enableTypes:["json","test","form"], onerror:function (err,ctx){ log.print("api service body parse error",err); ctx.throw(400,"body parse error"); }})); app.use(router.routes()); app.listen(g_port); console.log('Running a koa server at localhost: ', g_port) } main();Copy the code

The code is simple enough to quickly set up a Web server using the interface provided by KOA. Note that the sample code specifies the data to be transferred in JSON format when initialized.

2, test,

First run the server with node koa_test.js.js, and then listen on port 4000. Tip:

$ node koa_test.js
Running a koa server at localhost:  4000
Copy the code

Then run the curl command on another terminal. Since this article is testing under Windows, there are two Git bash openings. Use curl to send get

$curl http://127.0.0.1:4000/Copy the code

The server processes the response and returns the following message:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    16  100    16    0     0  16000      0 --:--:-- --:--:-- --:--:-- 16000
welcome to koa
Copy the code

Using curl to send a post request:

Curl http://127.0.0.1:4000/set - POST - H X "content-type: application/json" 3-d "{" CMD" : "set", "Content" : "foobar"} 'Copy the code

The server processes the response by simply printing the JSON data that curl passes:

[20121-08-08 16:25:48.337] Got: {CMD: 'set', content: 'foobar'}Copy the code

The return message is:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    41  100     9  100    32    145    516 --:--:-- --:--:-- --:--:--   661{"ret":0}

Copy the code

3, summary

Curl is lightweight and can handle simple testing situations. If it is a GET request, just pass the URL (plus port) and the URL address. In the case of a POST request, in addition to the URL, you need to specify the POST request with -x POST and specify the JSON format with -h “Content-Type:application/json” (this corresponds to the KOA setting). Finally, use -d to specify the JSON data, noting that each field of the JSON data requires parentheses, and the outermost curly braces {}. Otherwise, it cannot be properly parsed.