#23 Golang - Dynamic JSON & HTML Responses in Go with Gin Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] today we're diving into the Gin framework in go showing you how to dynamically render Json and HTML in response to a single request whether you're building an API or a web application this trick will make your server more versatile and responsive to different client needs in web development it's common to need different response formats depending on the client request some may need data in Json format for apis While others might require HTML for web browsers handling both in a single endpoint can streamline your application and reduce the need for duplicate Roots here we have a simple gin app this app displays a bunch of blogs in a paginated view this view is paginated we covered pagination in episode 9 by clicking on a Blog we land on the blog show page this app serves HTML Pages we will make changes in the code to make the same root server Json Also let's look at the code here is the main function these are the apis we have implemented let's look at the Handler functions in both these functions we serve HTML while making the request we need to send a parameter say format this parameter can be HTML or Json later in the video we will use extensions like Json or HTML to serve the appropriate format stay tuned in the Handler let's read the parameter format in a variable we will use context query method now put a switch statement on the format the first case is HTML move the HTML renderer within this case the other case is when the format is Json let's create a Json renderer here let's check if this works passing Json works the response is a Json similarly passing HTML works too let's remove the parameter format and it does not work we need to have a default renderer which is HTML let's change the query method to default query it takes the uis argument and the default value we pass a HTML as the default value restart the server now it renders HTML when the format is not passed having this logic in all rots is a pain so let's move this logic to a separate function create a new file u.o create a new data type called response format which is of type integer we will create constants for different formats the first one is no format this signifies the format is not specified the other two formats are Json and HTML now create a hashmap to map the format passed in the API to these con the map will have a string as a key and response format as the value Json string is mapped to format Json we do the same for HTML now we will create a function say get format we will use this function in the controllers to detect the format sent in the API it accepts gin context and returns response format here we will read the URI parameter format with the default value HTML as we did in the Handler function if there is no match of the format in the hashmap it will return zero which is no format this is similar to to our default format case so let's return HTML format else we return what the hashmap returns here we are reading the hashmap twice so let's read it once and put the value in a variable detected format now make use of this variable well there is more cleanup possible in case there is no format detected we send HTML what if we make the value of constant called format HTML zero this will remove the if else in this function let's change the value of format HTML to zero now this function is really simple let's make use of this function in the handlers change the switch case statement accordingly now use the same format detections and render the requested format in the other Handler function too restart the server now let's try the individual your blog show API it works in all cases it would be great if we could add an extension in the API like HTML or Json and it renders the format based on this extension we can add arguments to the apis let's add dot format to these apis restart the server we see an issue here with the second API since Jin does not support regular expression in the apis we cannot use two parameters next to each other then what is the solution Hine X comes to the rescue we can use it as a reverse proxy let's see how we can use it to detect format using the extension in the API I already have engine installed and have put configurations in place in the sites available directory I created a new configuration called gin blogs let's see what is there in this file this is the server block these director Ives tell engine X to listen for incoming connections on Port 90 for both ipv4 and IPv6 addresses respectively this Block matches all requests and contains the rules for processing them this condition checks if the requested URI ends with HTML or Json the API endpoint or file name without the extension in the first part is assigned to variable API the extension part is assigned to variable extension this directive rewrites the original request URI by removing the extension here this is replaced with/ API API is the endpoint we fetched earlier this adds a custom HTTP header X original extension to the proxy request contain aining the original extension either HTML or Json this header can be used by our server to determine the requested response format this directive forwards the modified request to the backend server running on Local Host port 8080 once the configuration is in place we make a Sim link of it in sites Ena directory for engine X to consider it this fails because we already have this Sim link restart engine X service now let's read this header we have added in engine X in our code in this function let's read the format from the header using get header function we need to set the default format to HTML if the format is blank set HTML as the format restart the server let's try the apis oops we need to use engine export which is 90 the Json extension Works HTML works too and no extension works well and that's how you handle Json and HTML rendering in a single request using Jin this approach can significantly enhance your application's flexibility and user experience if you found this tutorial helpful don't forget to like subscribe and hit that notification Bell for more coding tips and tricks happy coding
Info
Channel: codeHeim
Views: 1,256
Rating: undefined out of 5
Keywords:
Id: oGCwGhdjjIk
Channel Id: undefined
Length: 15min 25sec (925 seconds)
Published: Thu Feb 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.