How to manually test URL Shortener Microservice

Hello,

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:

image

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.

Challenge:
URL Shortener Microservice

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:
image

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:
{}
false

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

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?

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.

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.

FINALLY SOLVED IT

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:

image
image

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

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!