How to manually test URL Shortener Microservice


I’m trying to figure out how to test the URL Shortener Microservice project myself, but I’m struggling. I’ve tried creating a post request to [project_url]/api/shorturl/new but I don’t think I’m quite doing it right. Can someone help?

Here is the post request code I have so far:


I’ve found as much as I could online, but I think I’m missing some fundamental understanding since I’ve never written a post request before.

URL Shortener Microservice

How about something like the following. Make sure to click the open in link.

Hello Randell,

First off, thanks for your help! Even you providing that example has allowed me to see how to construct a post request.

However, I’m still running into an issue. When I try to access the url through the req.body.url, it is undefined. I investigated a bit and found that req.body is in fact an empty set of brackets {}. Because of this I can’t test my post method. Or maybe I am misunderstanding something?

I looked through your post request, and it seems like you placed everything in the correct spot so I should be able to access the url through req.body.url (the req variable is my name for the incoming request).

Here is a picture of my code trying to troubleshoot this:

Also, the console output logging the requrl variable and urlregex test (which should be true since the url is the proper google url) looks like:

Note: I would upload more pictures but I’m limited by freeCodeCamp

Do you have a link to your project on Glitch? I can look at your overall code and see if I can make some suggestions.

Sure. Here it is:

It’s near the bottom under the URL Shortener Microservice section

Hey Randell,

So I’ve been able to troubleshoot the post handler, and everything now works except for the body parser. Whenever I try to access the body of the post request, it comes up as undefined, which I think means that I have the incorrect body parser. I’ve been using the app.use(bodyParser.urlencoded({extended: false}) body Parser, but that isn’t working. I’ve also tried using app.use(‘input_path’, bodyParser.json()), but that is giving me an “Unexpected token u in JSON at position 0” error.

Which body parser should I use? Meanwhile I’ll keep trying different ones and see if any of them parse the post request correctly.

Update: The body of the request seems to always be empty so maybe the data in the post request isn’t getting written somehow? Do I have to give permission to URLs to access the post handler and that’s why the post data isn’t being properly written in the body?

If you are wanting to create a post request using JSON, then you must add:


Your project code is very unorganized. You really should be creating a separate Glitch project for each of the certification projects. Also, you only need to create a server.js file. You have so many unrelated code segments in your myApp.js that should not be there. It makes it very difficult for someone trying to help you decipher your code. Also, keeping the project code focused on a single project will make it easier for you to go back and review at a later date if you want to make changes.

Hi Randell,

Yes, I already tried doing that and I got an error saying “Unexpected token u in JSON at position 0”.

I created a new project and put only the relevant code in it, so this should be much easier to look at. Sorry for the initial mess!! I’m new to all this, so I’m still learning.

I added app.use(bodyParser.json()); to your Glitch project and then made a post request with the following JSON:

    "url": ""

It works as expected. You can look at the Glitch Logs to see your console.log(requrl) shows the following:

Hello Randell,

Thanks for your help! I’m still a bit confused though. Is my post request at lines 119-142 incorrect then?

Here is the response I’m getting in the log:

For some reason, it’s getting a response of an HTML file? I don’t get why this is happening at all. This is the first time I’ve come across this issue.


The issue was that I saved the post data to send as querystring.stringify when it needed to be JSON.stringify since the bodyParser.json() couldn’t reconcile the querystring object.

Here is how my code looked originally when it wasn’t working:


This is how it currently looks now that it’s working:

Basically, the post data format and the bodyParser format have to be the same in order for the bodyParser to correctly parse it. I overlooked this and that was the cause of my errors.

Thanks for all your help, Randell!