در حال بارگذاری ...

مرور کار با Postman0:00

I am a woodworker, and one thing I learned very early on is that the right tool makes all the difference in the world. And if you don't have that tool, yes, you can get around that, but your results aren't going to be as good, and it's definitely going to take you a lot longer to achieve whatever it is that you were trying to achieve. And the same goes for writing applications, especially web APIs, because we need a tool that we can use to make HTTP requests. And we have one. It's called the browser. In fact, in the previous lesson, we made a GET request for our login route, and we can

Need an HTTP debugger0:35

It's called the browser. In fact, in the previous lesson, we made a GET request for our login route, and we can see the response in the browser. But we don't need a GET request here. We need a POST request. And if we change this to POST, yes, we can write a form to submit that, or we can write some JavaScript, but that's going to take extra time. And really what we need is a tool that we can use to not just make HTTP requests, but also inspect the responses. We need what's called an HTTP debugger.

also inspect the responses. We need what's called an HTTP debugger. And there are many available, but one that I typically use is called Postman. Now, they do have some pricing tiers, but you can use Postman for free. And it is one of those applications where they want you to sign in. And when you do sign in, you get a lot of extra features than if you don't sign in. But you can use it however you want. Just download and install it. If you want to sign in, great. It gives you the ability to create a workspace.

Setting up Postman workspace1:36

If you want to sign in, great. It gives you the ability to create a workspace. If you are working on multiple APIs, you can create workspaces for those individual projects. I'm going to create a workspace. I'll call it Tickets, Please, which, by the way, this is for creating and managing support tickets. I don't think I mentioned that in the previous lesson, but that's the general idea. And once you have a workspace, just click on the plus sign up at the top, and this is going to create a new request. So let's make a request for our login route.

going to create a new request. So let's make a request for our login route. That was api and login. And we will see the result in the response. Except, no, we won't, because I just changed that to post, didn't I? Yes, I did. But that's one of the beautiful things about something like Postman, is that we can easily change the type of request. We now have a post request, we can send that, and we can see that we are getting the appropriate response.

Creating login request validation2:30

We now have a post request, we can send that, and we can see that we are getting the appropriate response. Now, of course, in order to log in, we need to supply some credentials. And we don't have anything as far as authentication is concerned yet. But we can go ahead and we can set this request up so that whenever we do have authentication, well, this will work. So let's make a request. We'll use artisan to make a request, and we'll call this APILoginRequest. And as far as a login request is concerned, we will essentially just have two pieces of information.

And as far as a login request is concerned, we will essentially just have two pieces of information. We'll have the email address and the password. So let's add some validation rules, because we need to be sure we have those pieces of information. Let's change the authorize to true, because if it's false, then the User is not going to be able to log in, because they will not be authorized to do that. So we will set that to true. And then inside of rules, we will have the email field, which is going to be required. Ideally, we would have some other validation here, and password is going to be required.

And then inside of rules, we will have the email field, which is going to be required. Ideally, we would have some other validation here, and password is going to be required as well. And then we can use this request inside of our AuthController. For our login method, we will type hint for the APILoginRequest. And instead of outputting hello login, let's do this. We will output the email address that was supplied. So we will get the email from the request, and we should see that. So let's go to Postman. Now we need to be able to send this information with the request, and we can do so by going

Sending credentials in Postman4:11

So let's go to Postman. Now we need to be able to send this information with the request, and we can do so by going to this Body tab here. And we want to set this to form data, and then we will set the keys and values. So our first key is going to be email. The value is going to be whatever email address that we want to use. Let's use jeremy@mcpeak.com. And then for the password, let's have password1234. And whenever we submit this, we should see the same structure, except that the message is now the address that we included.

And whenever we submit this, we should see the same structure, except that the message is now the address that we included. And of course, whatever we pass as the email address is going to be represented in the message that we get back from the server. But what happens if we provide the email address but not the password? Now remember that the password is required. So whenever we send that request, well, we can see that the status is 200, but that's not necessarily what we want. In fact, if we take a look at this, we can preview this, and we're going to see that we get what looks like a typical Laravel starting page.

Adding Accept JSON header5:19

In fact, if we take a look at this, we can preview this, and we're going to see that we get what looks like a typical Laravel starting page. Ideally, we would get back some kind of error. Well, we can, but what we need to do is tell the API that, hey, I'm sending you this data. I want you to send me back data as JSON. And we can do that by providing the Accept header with the request. So let's go to headers. Let's add the Accept header, and we want to accept application/json. So if we send this request, once again, now we get a JSON response. And we can see that the message is that the password field is required, and then it shows

So if we send this request, once again, now we get a JSON response. And we can see that the message is that the password field is required, and then it shows the errors that the password field is required. Now this might seem a little cumbersome because the client will always have to include that Accept header with every request. But most APIs require that anyway, because there are many that will return data in whatever format that you want. It could be JSON, it could be XML, it could be HTML. It of course depends upon the API, but this is not uncommon. But one thing we will have to remember going forward is that as we are testing our application

It of course depends upon the API, but this is not uncommon. But one thing we will have to remember going forward is that as we are testing our application and testing our routes, we need to ensure that we include the Accept header. Now one of the really nice things about Postman is that we have this workspace that we can then start saving requests to. So we have a request going to our login endpoint. So we can just save that. We can give it a name of loginRequest, and we could put it inside of a new collection, which we could call authentication. So that whenever we write a route for our register endpoint, we could essentially do

Creating register endpoint request7:06

which we could call authentication. So that whenever we write a route for our register endpoint, we could essentially do the same thing. So let's write a register method. For right now, we aren't going to have a request object, but we can return OK. And for the sake of simplicity, let's just return the message of register. Let's go to our API file so that we can handle the POST request for the register endpoint, and that is on our AuthController and the register method. So we can go back to Postman. We can create a new request.

So we can go back to Postman. We can create a new request. It is a POST request. Instead of login, we want to make a request for register. We can send that. We will see the response. We have the message of register. The status was 200. And if we wanted, we could save this request so that it is the registration. And what saving this request does is gives us easy access to come back and make these requests.

And if we wanted, we could save this request so that it is the registration. And what saving this request does is gives us easy access to come back and make these requests over again so that any time that we want to test our login or our registration routes, we can go to our collections, go to authentication, and just double click on login or registration, and it will open up that request. So all we have to do is then provide whatever body and then send it, and we could see the response. So for example, if we open up login, we should see inside of the body that we have the email and the password. So we do. We can send, and it just gives us easy access to make those requests.

So we do. We can send, and it just gives us easy access to make those requests. Now, we are going to make extensive use of Postman because, really, there's no better way to actually test a web API. I mean, yes, we can write tests inside of our application and run them, but there's nothing better than actually making the HTTP request, seeing the response, and having that actual confirmation that it works.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟