Laravel API | All about HTTP client

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
assalamualaikum guys in this video we'll talk about with HTTP clients and a lot of it so let's start first of all htmp client is a minimal ipoi for guzzle HTTP client so if we open this link here as mentioned Android documentation it implements or make it easy for using this guzzle with a lot of it and ways away guzzle also Implement skill and streams and sockets for bhcb and save this time also so it's to make easy for easy thing so let's start I will use this ajson list holder for sending requested get request post and so on so here in my controller I have critics controller called learn HTTP controller and here I have created this end point so if I go here and if I open it now user.test it works correctly and I will open learn http it works also so the first thing here I will send I get request for getting a post so I will say here response and using HTTP I will disable co-pilot by the way right using hdb I will use it automatically here this facade and I will say get method because I am using get right and now I will paste the URL here this URL for getting a post this one here and if I open it in a new tab as you can see it returns only one post and here for example two three and so on so I will use this tutorial here and because it returns Json I will say Json method irrigation and now let's Dian dump the response to see the result send request here and as we can see it returns user id1 was ID number one and work is correctly what is the difference between Json and body here the body method return is string so if I make it body here refresh as we can see it's completely string not Json right and also by the way it's for Json it returns array X or implements array axis interface which means here this one we can directly even with our Json if it returns This One automatically we can directly use a arrays here so if I say ID for example because it implements array axis if I said a user ID written user ID for example a title I think as we can see here so it automatically implements array in case of returning Json right also if you want to learn the status of the request or of the response we can use status method so if I go here and I will say status now refresh the written as 200 which means success right also we can convert the response to collection using connect method so I will say here collect and when I refresh here is the collection has been created with this data here so now I can say for example first and using methods of connection or even saying for example here a title workers also right also we can check if the response is successful or not using successful and failed message so we can say here successful itresense Boolean through a field it will return in false because its success and everything is 200 and also we can get the headers of the response using headers method here and as we can see some of array content type is application Json as we can see and there is limit here also we can check also the redirection so if we see here redirect this method and it doesn't redirect to anything because it is 200 not a 3xx and also we can check if the error is a 400 or in this way or a client error or not so if I say here is it a client error client error refresh it's false because its return is a 200 and success but if I go here and for example I will say boss to ID number 10 000. as we can see it attains 404 but what if we trade it with 10 000 here I will say also ten thousand now refresh now it's true right because it written is 404 here and now if we check the status check here it's 404 right here it is about receiving the response or formatting the response from HTTP facade the next thing here is a sending or posting data here and by the way before moving to it actually I want to continue with using defined status methods so if we go here to a lot of documentation instead of checking if the a status is 200 and checking by number actually we have some predefined methods here represents the keys or the meaning of this status codes so for example we can say here and support for non-existing root here we can say as sorry not found right which is Newton is 404 if we open this note found as we can see the status equal to 404 and here of all of these functions here so if we back here again and send request it's true right it returns Boolean by the way and for example we can check any other error here by using these methods and also it came in 200 so if I fix it again here and I will say okay check Etc also it works because I fixed the URL the next thing here if we want to use Query parameters for example so I want to say user or here something like this a user ID equal to one it works also but what if for example the URL has multiple query bombs so it will be a long string so instead of that we can say here if we use get method we can use an array right second parameter is a query parameter so we can say user ID equal to one it's the same for a user ID here equal to one the same result so now let's try and I will then position here back and refresh and as we can see it returns 10 posters for user ID number one here right because also it exists in the documentation here if we say here in filter resource filtering results here query parameter user ID number one and now for example it's converted to a collection using a collect method and I want to let's first see the result here collection also of 10 items I want to return this first element only or first post only Network so we can use first method for collection so refresh and it Returns the first post here and also we may use where maybe ID equal to 10 for example and I will say also first to close its connection now refresh the same result for example what if I set it 10 000 it doesn't exist so it will return null right now let's move to post request or posting data right so I will comment it again here and I recommend this data actually I will remove all of them and now for posting requests we can say here response again it spawns and for http class post and here is the URL for creating this link here and the second barometer for post method here is the data we want to post or create so our keys as suggested here this data title body and user ID so I will say title is hello world for example and user ID equal to one and body is for example thank you and now this is the undump the response here sponsor status to check if it works or not now refresh the page and it is 201 which means created so if I check here using created method if I open it it implements also the status equal equal to 201 instead of using f and it returns true and if you noticed here the headers content type must be application Json which is by default when sending a request which is application Json so what if we want to change this content type for example we can use method here this method before sending by the way we can say content type and visiting the content type we want to set I may say it for example a plane slash text and now let's see what will happen no change it works correctly also if you want to change it to maybe a using Json we can say as Json for sending Json data so if we want as Json method here it sits as a content to type as we can see two applications less Json as it said automatically here right the same result now let's try again to work correctly Sometimes some API is requires a sending form request and attaching this content type now we can use as form method so if I back here again and I say as form is it check and also where it works correctly so here's some advice requires Json as a content type and others required a urine decoded as it suggested here as we can see here form URL encoded also we may need to send a tokens or making authentication so we can say here with token and instead of sending a header here so we can see with talking it seems better token by the way so it's for example representing this string here so if I want to send the token as any random data here it will send it in header authorization as this string Bearer here plus our sync and that will make it easy using with token method also some apis requires basic authentication by using username and password directly so we can say here a basic with basic authentication and it takes the parameter of username and password if you don't know what is the basic course is we can say here basic authentication it's a prompt actually authentication here yes as we can see sometime when you open some sites it's required authenticating in the prompt of JavaScript as we can see here actually it's not a JavaScript it's prepared by the web server like nginx or Apache so this data can be prepared by the server and it requires username and a password and in a lot of it actually maybe in larval Horizon for accessing your holiday Resort monitor it requires the building basic authentication also so here's what does basic authentication mean right the next part here is a retries so sometimes may the request failed and when it's failed we want to make it retrace automatically so we can say here a retry method and as we can see it takes two parameters the first parameter is how many times beer milliseconds so I want to say here try it three times each 500 second or milliseconds or each half second in case of failing we can use when also for uh using Boolean or closures and this is strong method through this error so if we want to disable a throwing error we can say through and set it to false for uh not throwing any error and continue implementing the code if you don't want to throwing any exceptions we can also set that time out of request so enlar Levels by default the timeout is 30 seconds here but we can set it also for one second or any number of seconds you want this integer by the way right but by default it's 30 seconds also if you want to adding headers when sending request we can say here with headers method using with headers it's an array and the key is the header and its value so for example we can use it actually here again this value or you can say content type here and its value is application slash Json here and also we can say it's actually one value here I will continue adding semicolon and let's try to check again here refresh and the same reason right so I want to check out the Edison or the response of this request after creating a post so it Returns the post that we have created here hello world and thank you and it works correctly what if we change it here the URL now it returns node and if I will check field it will return it rule equals the URL is corrupted and we can say here we can check also is it a server error or not using server error silver only means a 500 XX here so if we open it as we can see less than or equal 500 so for each again it's not a server error it's a client error client error because its status is 404 I think right one last thing here is throwing exceptions so we want to let users know that this URL doesn't work so if I send the requester here it doesn't show anything so for example I want to say for response here through if client error right now refresh and now it's with error HTTP request to return it 404 or we can say through if and using any logic we want for example if the response status equal equal to 404 again the same result but what if the status for example through error if the status is 400 only it will not throw anything right another method here I think is is through if status also if States is equal to 400 through it if not not through it so I want to for example so if the status is 404 send and the same result we can also using through method for implementing our own exception so we can say here function here and if we check again it must slow request the exception or we may say here a port a 404 Maybe now refresh and as we can see it throws 404 we may approaching it 403 and forbidden maybe 400 only and it's a bad request right so you can Implement also your own exception or throwing your own exceptions that you want here we can dump also our a request here using DD before sending so as we can see here's our request data right the method and there is our data also that we will send and our headers here inform the content type by default or here we set it with headers our headers here what if we change it to this data for example here is our request again headers and here is what we have set and as you can see here by default the HTTP errors is false because it's not throwing errors unless you use through error and by the way if you use only response through by default it will through error in case of failing without implementing any closures here so if I try again now here I will remove DD refresh again now automatically choose the error that received from the API if I take magnetia again as we can see here is the default to value of laravel for printing a request here so the timeout is 10 for connection time out they are different by the way and the timeout is by default as we mentioned here it's 30 seconds and throwing errors here is false so it will not throw anything in case of getting any errors right one last thing here before finishing the video because the HTTP client implementes that doesn't HTTP client or aibi we can also set ourgasm options here inside our HTTP client or HTTP in a lot of using with options message so if you use gazelle request options or if you want to use one of them of this data here you can use them by using with options method here all of these options right of gazelle and if you want to use them you can use them using with options method by using the smooth here add a key value and the source is from a gazelle API here is quickly how can we use HTTP client in laravel if you found this useful support to me by clicking on like share and subscribe see you in next videos
Info
Channel: Laravel boy
Views: 2,207
Rating: undefined out of 5
Keywords: Laravel, learn laravel, php, laravel boy, advanced laravel, laravel tips and tricks, laravel tips, laravel advanced tips, learn php, laravel, PHP, web, laravel helpers, master laravel, laravel route, controllerless route, laravel boy route, laravel validation tips, advanced laravel valiadtion, json response, resource in laravel, laravel http client, laravel guzzle, laravel curl
Id: fEZG-Esg4-c
Channel Id: undefined
Length: 20min 6sec (1206 seconds)
Published: Mon Jul 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.