Check out a free preview of the full Build Go Apps That Scale on AWS course

The "Testing with cURL" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:

Melkey tests the API from the command line using cURL commands. A username and password are sent to the API using a POST method. If CloudWatch Logging errors appear when deploying the application, the `LoggingLevel` property in the go_cdk.go file can be removed, or the instructions mentioned in the lesson can be followed.

Preview
Close

Transcript from the "Testing with cURL" Lesson

[00:00:00]
>> So we just deployed our API gateway. And if you look at the deployment, there's actually something super interesting. We got an endpoint. We have an actual endpoint that we can query and check things. So remember I told you earlier, I wanna do everything from the CLI. That's kinda my style.

[00:00:17]
So I want to just curl it from the CLI. And you can use something to help you do this. You could use Postman, or Insomnia, or another tool. Postwoman, I think there's a bunch of different ones. Call me old-fashioned like prime, I just wanna curl it, okay? But before we do that, it's always good to double check, triple check what's going on in cloud formation in the resources.

[00:00:41]
So here, our table looking nice, LambdaFunction, looking nice. API Gateway, sweet, this is new. If we go in and click this, we should see our defined methods there. There you go. So /login and /register, we can post both of them. This is very good. So there is some confirmation that we've deployed something that resonated with AWS resource to allocate what we've wanted to, right?

[00:01:09]
The only question is, did it work? And again, this is kinda that pain point of that iterative deployment, iterative speed. This is where local deployments will definitely come into play. But for our case here, I should not have cleared that. I should not have cleared that. This will be quick, I have to redeploy it.

[00:01:27]
But because we're not spitting up infra, it's just gonna quickly just give me, the stack is gonna check, the diff is gonna be the exact same. It's gonna give me the endpoint. I accidentally cleared the endpoint. There you go, that's all I needed. So this endpoint, we're actually gonna call it.

[00:01:42]
So copy, so curl. If you've never done curl request like this, I'm gonna maybe show you something new, -X POST. We're gonna paste the link that we just got. And the endpoint is going to be register, okay? And I'm gonna add a header, so -H, double quotes. I like to, when you create something on the CLI, cuz you don't get the autocomplete.

[00:02:06]
If you do something open, do it close in the next character. So if you do an open parentheses, close it right away. Don't open, fill it out, and then close. You'll forget. So here, just open, close the quotations. And we'll do Content-Type, capital T. Content-Type is application/json, right?

[00:02:30]
We know this is something that we want, cuz we told our API gateway of the Content-Type header, right? So we know this exists. And then we need the actual body, so -d, little d. And then we'll do single quotes, these curly brackets. And then we'll put in username.

[00:02:49]
And then my username, let's say marcG. And then here, we'll put password. I'll put, ilovePrime.
>> You have a typo in Content-Type. There should be an N in there, the Content-Type header.
>> There should be what?
>> Content-Type is just spelled wrong, it says contet.
>> Yes, thank you, thank you.

[00:03:14]
Again, I can't type, Content. I'm gonna double check this right now, curl -X POST, the endpoint with appended /register, header, big H, Content-Type, application/json. And then we put in the payload, little d. We've got everything closed, looks good. Anyone see anything? Do we run this? Okay, this is the moment of truth.

[00:03:47]
Sweet, successfully registered user, I'm very happy. [LAUGH] Okay? And if we refresh this, marcG with the hash password, let's go. Sweet, it's all coming together. Now, I don't know his password is ilovePrime. I don't look at his accounts. I just know that by knowing it. But now, no one else will know that information.

[00:04:13]
Let's log in. And the really cool thing about doing it in curl, it's like you can just go up then change /register to /login. This is also a very stressful point. Cuz if login doesn't work, we have to debug live. All right, do you think it's gonna work for us, T?

[00:04:35]
We got a shrug. Okay, successfully logged in. Cool, but this could be a false positive. Let's go back up and let's change ilovePrime to iloveDustin. Let's see if this works, right? We change the password of an already registered user, invalid user credentials. So there you go, we have a way to register users and login.

[00:05:01]
And that's pretty cool, right? I totally think this is, maybe it's simple, it is. But in terms of using this AWS infrastructure, I'm a big fan of this. So yeah, I mean if you're still here, if you're still not checked out, good. [LAUGH] I don't know what to say.

[00:05:19]
But any questions on, yes?
>> Just one thing, the configuration line about logging, further up.
>> Further up?
>> When we're setting up the API, the NewRestApi bit, the stack function.
>> You mean here? Yeah, yeah.
>> No, sorry, in the go_cdk.go.
>> Yep, we're going right here?

[00:05:42]
>> Yeah.
>> Yep.
>> Keep going up.
>> Keep going up?
>> Right there, the DeployOptions LoggingLevel.
>> Yeah?
>> That busted my deploy.
>> Really?
>> So I erased it, and it worked fine.
>> Well, do you remember what the error was?
>> It's CloudWatch Logs role ARN must be set in account settings to enable logging.

[00:06:01]
>> You got it too?
>> I commented it out-
>> Okay.
>> Instead of setting it up properly.
>> Interesting, I don't remember if I set anything up differently to CloudWatch to get the LoggingLevel. Maybe I did spin up CloudWatch way long ago that has this enabled. But yeah, I should have stated that the MethodLoggingLevel goes directly to CloudWatch.

[00:06:22]
So that's how we kinda see the source of truth of, okay, where's the errors that are in CloudWatch? But yeah, if you have to comment it out, then yeah, comment it out. But thank you for raising that, cuz I'm pretty sure other people had that problem too.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now