Laravel API Master Class - Working with Postman

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 see the response in the browser but you know we don't need a get request here we need a post request and if we change this to post you know yes we can U write a form to submit that or we can write some JavaScript but you know that's going to take extra time and really what we need is a tool that we can use to not just make HTT requests but 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 uh just download and install it if you want to sign in great uh it gives you the ability to create a workspace so 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 U 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 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 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 artisen to make a request and we'll call this API login request 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 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 oth controller for our login method we will type hint for the API login request and instead of outputting hello login let's do this we will output the email address 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 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 mp.com and then for the password let's have password 1 2 3 4 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 larel 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 Jason 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 SL Json so if we send this request once again now we get a Json response and we can see that the message is at the password field is required and then it shows 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 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 login request 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 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 okay 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 off controller and the register method 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 over again so that anytime 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 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 to run them but there's nothing better than actually making the HTTP request seeing the response and having that actual confirmation that it works
Info
Channel: Laracasts
Views: 2,425
Rating: undefined out of 5
Keywords: web development, php, programming, laravel, laracasts, jeffrey way, coding
Id: SIoeU1x7xok
Channel Id: undefined
Length: 9min 6sec (546 seconds)
Published: Mon May 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.