Time and again I’m wiring up an integration with some JSON API and the end point isn’t online, or it’s out of my control and I can’t manipulate the responses. Luckily json-server is here to give us easily manipulated JSON responses to our HTTP GET requests.
However, many (I might even say most) JSON end points are not fully RESTful, and json-server is REST only by default. Google’s reCaptcha verification API is a good example of an end point that you POST to but expect a response body from. In fact, json-server is actually pretty bad at faking a POST end point out of the box because POSTing to it actually modifies to underlying json file.
Buried in the closed issues for json-server’s GitHub is a partial set of instructions for modifying json-server to treat POSTs as GETs and give me what I want: a simple dummy end point that responds to a POST with whatever I tell it to respond with.
The following are the instructions I’ve been using for completing this task:
Install json-server
You can get json-server from https://github.com/typicode/json-server
As of this writing the install steps are as follows:
- Install Node.js and npm if you don’t have it yet. Instructions [here](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
- Run
npm install -g json-server
Create a script to switch POSTs to GETs
Create the following middleware file in your working directory. I named mine middleware.js
module.exports = function (req, res, next) { if (req.method === 'POST') { // Converts POST to GET and move payload to query params // This way it will make JSON Server that it's GET request req.method = 'GET' // This line was causing me problems and my needs here are simple enough that I don't need it // req.query = req.body } // Continue to JSON Server router next() }
Create your test json database
For reCaptcha, I created the following test database json file and named it fake-captcha.json
{ "success": { "success": true, "challenge_ts": "2018-03-09T10:11:32EST" }, "failure": { "success": false, "challenge_ts": "2018-03-09T10:11:32EST" } }
Note that json-server requires the properties of the top level object to be objects or arrays of objects. I initially attempted to put an unnamed response in the top level object. As far as I can tell that does not work (the generated end point will not respond to GET or POST requests for /).
Run json-server
Run
json-server -p 50505 -m middleware.js -w fake-captcha.json
Note that I have something else running on port 3000 making the -p necessary.
Test your end point before use
I use Postman for this but you can also just use curl
.
Do a test POST to http://localhost:50505/success to make sure everything is good.
You should get back the following response:
{ "success": true, "challenge_ts": "2018-03-09T10:11:32EST" }
Do a test POST to http://localhost:50505/failure to make sure everything is good.
You should get back the following response:
{ "success": false, "challenge_ts": "2018-03-09T10:11:32EST" }
Now you can configure your backend service to point to one of those two end points above to test your reCaptcha validation!
If you’re double checking the challenge_ts you can either modify the file’s time stamps manually or write a little script to keep them up to date.
Conclusion
The great thing about this is that now I can use json-server for faking basically any end point that responds with a JSON body. This will allow me to write code against end points that I don’t have access to yet, or even end points that aren’t finished.
Leave a Reply