@@ -319,7 +319,7 @@ Now, we can pass the variable to thq request like so:
319
319
320
320
'''console
321
321
$ *req* run users --page 2
322
- GET *https://reqres.in/api/users?page=2*
322
+ GET *https://reqres.in/api/users?page=2*
323
323
*Status*: 200 OK
324
324
*Headers* |(14 hidden)|:
325
325
age: 5997
@@ -358,7 +358,111 @@ $ *req* run users --page 2
358
358
This time, we can see the second page of results.
359
359
You can learn more about variables in 'req help variables'.
360
360
361
- Now, lets try creating a flow.
361
+ Now, lets try creating a flow. Suppose you have an API at 'https://localhost:3000/'
362
+ where you can log in and sign up. Lets create requests and a flow for that.
363
+
364
+ Open up '.req/login.http' in your text editor and add this:
365
+
366
+ '''http
367
+ POST https://localhost:3000/login HTTP/1.1
368
+ Content-Type: application/json
369
+
370
+ {
371
+ "username": "{username}",
372
+ "password": "{password}"
373
+ }
374
+ '''
375
+
376
+ Now, lets create the signup request in '.req/signup.http':
377
+
378
+ '''http
379
+ POST https://localhost:3000/signup HTTP/1.1
380
+ Content-Type: application/json
381
+
382
+ {
383
+ "username": "{username}",
384
+ "password": "{password}",
385
+ "email": "{email}"
386
+ }
387
+ '''
388
+
389
+ We can also create a third request to view user info:
390
+
391
+ '''http
392
+ GET https://localhost:3000/user HTTP/1.1
393
+ Authorization: Bearer {token}
394
+ '''
395
+
396
+ Let's test these out.
397
+
398
+ '''console
399
+ $ *req* run signup --username user --password pass --email "user@place.com"
400
+ POST *https://localhost:3000/signup*
401
+ *Status*: 201 CREATED
402
+ *Headers* |(14 hidden)|:
403
+ age: 5997
404
+ *Body* |(application/json)|:
405
+ {
406
+ "username": "user",
407
+ "email": "user@place.com",
408
+ "password": "pass"
409
+ }
410
+
411
+ $ *req* run login --username user --password pass
412
+ POST *https://localhost:3000/login*
413
+ *Status*: 200 OK
414
+ *Headers* |(14 hidden)|:
415
+ age: 5997
416
+ *Body* |(application/json)|:
417
+ {
418
+ "token": "sdfj30J02fj29f2p"
419
+ }
420
+
421
+ $ *req* run user --token sdfj30J02fj29f2p
422
+ GET *https://localhost:3000/user*
423
+ *Status*: 200 OK
424
+ *Headers* |(14 hidden)|:
425
+ age: 5997
426
+ *Body* |(application/json)|:
427
+ {
428
+ "username": "user",
429
+ "email": "user@place.com",
430
+ }
431
+ '''
432
+
433
+ Thats cool, but we need to run three requests to get the user info. We can
434
+ automate this by creating a flow. Lets create a flow called 'auth' in
435
+ '.req/auth.flow.js':
436
+
437
+ '''js
438
+ (async () => {
439
+ const {username, password, email} = variables;
440
+
441
+ console.log("Signup with", username, password);
442
+ const signup = await run('signup', {username, password, email});
443
+
444
+ console.log("Login with", username, password);
445
+ const login = await run('login', {username, password});
446
+ const {token} = login.body;
447
+
448
+ console.log("Fetching user data with token", token);
449
+ const user = await run('user', {token});
450
+ console.log("User data", user.body);
451
+ })()
452
+ '''
453
+
454
+ This flow runs all three requests at once. Try running it:
455
+
456
+ '''console
457
+ $ *req* run auth
458
+ Signup with user pass
459
+ Login with user pass
460
+ Fetching user data with token nggypnglydngraad
461
+ User data {
462
+ "username": "user",
463
+ "email": "user@place.com"
464
+ }
465
+ '''
362
466
` ) ,
363
467
flow : makeDoc ( `
364
468
Flows are JavaScript files which tie together requests. You can use flows to
@@ -371,12 +475,12 @@ All flows have access to
371
475
2. The 'run' function, which can be used to run other flows and requests. When
372
476
running a requst his function returns a Promise which resolves to the
373
477
response, which is an object containing the status, headers, and response body.
374
- When running a flow, this function returns the value the flow returns.
478
+ When running a flow, this function returns the value the flow returns.
375
479
376
480
A sample flow looks like this:
377
481
378
482
'''javascript
379
- (async () => {
483
+ (async () => { // This is not necessary and is only used for async/await
380
484
await run("register", {username: "morpheus", password: "matrix"})
381
485
const {body: {token}} = await run("login", {username: "morpheus", password: "matrix"})
382
486
const user_data = await run("user", {token})
0 commit comments