<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bishal Shrestha</title>
    <description>The latest articles on DEV Community by Bishal Shrestha (@iambstha).</description>
    <link>https://dev.to/iambstha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1061146%2F4966828c-c46b-419a-8ac2-85de918f1f8e.jpg</url>
      <title>DEV Community: Bishal Shrestha</title>
      <link>https://dev.to/iambstha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iambstha"/>
    <language>en</language>
    <item>
      <title>@Controller vs. @RestController</title>
      <dc:creator>Bishal Shrestha</dc:creator>
      <pubDate>Wed, 15 May 2024 18:17:18 +0000</pubDate>
      <link>https://dev.to/iambstha/controller-vs-restcontroller-32n2</link>
      <guid>https://dev.to/iambstha/controller-vs-restcontroller-32n2</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgmsei14f465qu7judpeg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgmsei14f465qu7judpeg.png" alt="Controller vs. RestController Annotation Cover Image" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
In the realm of Spring framework development, distinguishing between traditional Spring MVC controllers and RESTful web service controllers is pivotal, particularly in their approach to handling HTTP response bodies.&lt;/p&gt;
&lt;h2&gt;
  
  
  Traditional Spring MVC Controller:
&lt;/h2&gt;

&lt;p&gt;In the conventional Spring MVC paradigm, controllers rely on view technology, necessitating a distinct approach to managing HTTP response bodies compared to the more modern RESTful web service controllers.&lt;/p&gt;
&lt;h2&gt;
  
  
  RESTful Web Service Controller:
&lt;/h2&gt;

&lt;p&gt;Conversely, RESTful web service controllers are designed to return objects directly, with their data serialized into the HTTP response, typically formatted as JSON or XML. The introduction of the &lt;code&gt;@RestController&lt;/code&gt; annotation in Spring 4.0 streamlined the creation of RESTful web services by combining the functionalities of &lt;code&gt;@Controller&lt;/code&gt; and &lt;code&gt;@ResponseBody&lt;/code&gt; annotations.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding Annotations:
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;@ResponseBody&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;To grasp the distinction between &lt;code&gt;@Controller&lt;/code&gt; and &lt;code&gt;@RestController&lt;/code&gt;, it's crucial to comprehend the role of the &lt;code&gt;@ResponseBody&lt;/code&gt; annotation. This annotation signifies that the return value of a method should be bound to the web response body, facilitating automatic serialization of the returned object into the HTTP response.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;@Controller&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;@Controller&lt;/code&gt; annotation, a specialization of the &lt;code&gt;@Component&lt;/code&gt; annotation, enables automatic detection of implementations through classpath scanning. When employing &lt;code&gt;@Controller&lt;/code&gt;, it's imperative to include &lt;code&gt;@ResponseBody&lt;/code&gt; in each &lt;code&gt;@RequestMapping&lt;/code&gt; method to serialize the return value properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.stereotype.Controller&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RequestMapping&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.ResponseBody&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/v1/api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GreetingController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/greet"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@ResponseBody&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;@RestController&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Introduced to simplify the development of RESTful web services, the &lt;code&gt;@RestController&lt;/code&gt; annotation serves as a specialized form of &lt;code&gt;@Controller&lt;/code&gt;. It eliminates the need for manually adding &lt;code&gt;@ResponseBody&lt;/code&gt; to each &lt;code&gt;@RequestMapping&lt;/code&gt; method, as it inherently combines the functionalities of both &lt;code&gt;@Controller&lt;/code&gt; and &lt;code&gt;@ResponseBody&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.GetMapping&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.web.bind.annotation.RestController&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/v1/api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GreetingRestController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/greet"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaways:
&lt;/h2&gt;

&lt;p&gt;While both &lt;code&gt;@Controller&lt;/code&gt; and &lt;code&gt;@RestController&lt;/code&gt; can be used together, they cater to different needs – &lt;code&gt;@Controller&lt;/code&gt; for rendering views and &lt;code&gt;@RestController&lt;/code&gt; for returning domain objects as JSON or XML.&lt;/p&gt;

&lt;p&gt;Original post appeared &lt;a href="https://bishalshrestha.substack.com/p/controller-vs-restcontroller" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>springboot</category>
      <category>controller</category>
    </item>
    <item>
      <title>How to sort this list of strings along with the strings and output the result as expected?</title>
      <dc:creator>Bishal Shrestha</dc:creator>
      <pubDate>Fri, 29 Mar 2024 04:35:27 +0000</pubDate>
      <link>https://dev.to/iambstha/how-to-sort-this-list-of-strings-along-with-the-strings-and-output-the-result-as-expected-5827</link>
      <guid>https://dev.to/iambstha/how-to-sort-this-list-of-strings-along-with-the-strings-and-output-the-result-as-expected-5827</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/78236140/how-to-sort-this-list-of-strings-along-with-the-strings-and-output-the-result-as" rel="noopener noreferrer"&gt;
            How to sort this list of strings along with the strings and output the result as expected?
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Mar 28 '24&lt;/span&gt;
            &lt;span&gt;Comments: 15&lt;/span&gt;
            &lt;span&gt;Answers: 1&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/78236140/how-to-sort-this-list-of-strings-along-with-the-strings-and-output-the-result-as" rel="noopener noreferrer"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          0
        &lt;/div&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;When the input is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;["xL01(F]J","2pn5Mm","-5)8gF{","KWq0P]*%Q","n@,:\u003eAm@","\u003cRN_qCa7","8Qx\u0026RAON","gT~s!1s?4i{K","w\"r^d_#l$Mmp"]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Expected output should be:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;["(01FJL]x","25Mmnp",")-58Fg{","%*0KPQW]q",",:\u003e@@Amn","7\u003cCNR_aq","\u00268ANOQRx","!14?KTgiss{~","\"#$M^_dlmprw"]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are specific Unicode characters (\u003e, \u003c, \u0026) that were being sorted and replaced in a specific way. How to create a solution that could handle these Unicode characters dynamically without explicitly checking for each one individually. By identifying the…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/78236140/how-to-sort-this-list-of-strings-along-with-the-strings-and-output-the-result-as" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Question&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>java</category>
      <category>string</category>
      <category>unicode</category>
    </item>
    <item>
      <title>HTTP GET &amp; POST Request in NextJS Stable App Router</title>
      <dc:creator>Bishal Shrestha</dc:creator>
      <pubDate>Wed, 02 Aug 2023 08:53:09 +0000</pubDate>
      <link>https://dev.to/iambstha/http-get-post-request-in-nextjs-stable-app-router-557m</link>
      <guid>https://dev.to/iambstha/http-get-post-request-in-nextjs-stable-app-router-557m</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyx4bzbvzz3y5cyjtd2bg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyx4bzbvzz3y5cyjtd2bg.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
When the &lt;u&gt;app router&lt;/u&gt; was released in &lt;a href="https://nextjs.org/blog/next-13-2#custom-route-handlers" rel="noopener noreferrer"&gt;NextJS 13.2&lt;/a&gt; , being a frontend developer, I was stuck with building API's for my project. But &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;NextJS&lt;/a&gt; solved it finally by letting us create API endpoints for our application. Personally, I think building something with this in large scale is still a no-time-soon for now, but for small projects &amp;amp; frontend developers, this was a dream come true.&lt;/p&gt;

&lt;p&gt;In this tutorial, I am not going to cover detail topics on how to use route handlers and all. I hope you know the basics. One thing I was stuck with was handling form data for POST request. So, I am going to explain that in a second. But let's start with the installation and other stuffs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Latest version of NodeJS must be installed.&lt;/li&gt;
&lt;li&gt;Basic knowledge of HTTP requests will be added benefit.&lt;/li&gt;
&lt;li&gt;Understanding of useState() hook in react.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;After installing &lt;a href="https://nodejs.org/en/download" rel="noopener noreferrer"&gt;NodeJS&lt;/a&gt;, open a terminal and navigate to a directory of your choice using &lt;code&gt;cd path/to/project/directory&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then start a next app using &lt;code&gt;npx create-next-app@latest usingapidirectory&lt;/code&gt;, you can name your project name anything you want, I have named it &lt;code&gt;usingapidirectory&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get inside the project directory using &lt;code&gt;cd usingapidirectory&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, let's get the app running by using &lt;code&gt;npm run dev&lt;/code&gt; in your project directory in the terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open &lt;a href="http://localhost:3000/" rel="noopener noreferrer"&gt;http://localhost:3000/&lt;/a&gt; in your browser and see the result.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Code Editing
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open your project in the code editor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the app directory, open the &lt;code&gt;page.js&lt;/code&gt; file and replace everything with:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client"
import { useState,useEffect } from "react"

export default function Home() {
  return (
    // Your code goes here
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Also, for clarity, delete everything from &lt;code&gt;globals.css&lt;/code&gt; and replace it with:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Defining our state and form layout
&lt;/h3&gt;

&lt;p&gt;We will be building a simple form that accepts to fields: name and age. So let's define a state for that.&lt;br&gt;
Inside the app/page.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client"
import { useState, useEffect } from "react"

export default function Home() {
  const [name, setName] = useState('')
  const [age, setAge] = useState('')
  return (
    // Your code goes here
    &amp;lt;div className=" flex flex-col justify-center items-center w-full p-8 "&amp;gt;
      &amp;lt;h1 className=" w-full text-center m-4 font-semibold text-lg "&amp;gt;GET &amp;amp; POST Request in NextJS Stable App Router&amp;lt;/h1&amp;gt;
      &amp;lt;form className=" flex w-full flex-col justify-center items-center "&amp;gt;
        &amp;lt;div className=" flex w-1/2 justify-center items-center gap-4 "&amp;gt;
          &amp;lt;input
            type="text"
            name="name"
            placeholder="Enter the name"
            onChange={e =&amp;gt; setName(e.target.name)}
            className=" border p-2 px-4 rounded outline-none "
          /&amp;gt;
          &amp;lt;input
            type="number"
            name="age"
            placeholder="Enter the age"
            onChange={e =&amp;gt; setAge(e.target.name)}
            className=" border p-2 px-4 rounded outline-none "
          /&amp;gt;
          &amp;lt;button 
          type="submit" 
          className=" border-blue-500 bg-blue-500 text-white p-2 px-4 rounded-md " 
          &amp;gt;Submit&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a basic layout of our form that we are going to use. Styling is just to make it beautiful, don't bother if you don't want to.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating API
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, create a folder named &lt;code&gt;api&lt;/code&gt; inside the app directory. This is a must and if you are like "What?", "Why?", "How?", then I am pretty sure you should not read below this, first see the NextJS documentation on &lt;a href="https://nextjs.org/docs/pages/building-your-application/routing/api-routes" rel="noopener noreferrer"&gt;app routes&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the api directory, create a folder named &lt;code&gt;handleform&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now this is how you basically create an API endpoint in your NextJS application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your api is available at &lt;code&gt;/api/handleform&lt;/code&gt;. Sorry, not now. Wait!!!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's go. &lt;/p&gt;

&lt;h3&gt;
  
  
  Disclaimer! Did somebody said GET this.
&lt;/h3&gt;

&lt;p&gt;Handling GET request is extremely easy. For this project we are not going to use GET request since we want to handle form data using POST request. But here's a code that goes that will create an api get request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NextResponse } from "next/server";

export async function GET(){
    const data = {
        name: 'Bishal Shrestha',
        age: '27'
    }

    return NextResponse.json({data})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can check in postman or any other api testing tool and see for yourself. http:localhost:3000/api/handleform&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling POST request
&lt;/h3&gt;

&lt;p&gt;I told you earlier, this is what we are for in this tutorial. When I was learning NextJS, I got stuck here for like forever. Don't worry, you are not going to because I am here.&lt;/p&gt;

&lt;p&gt;Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Have you never ever used POST request in Next app?&lt;/li&gt;
&lt;li&gt;Are you stuck in POST request handling request.body?&lt;/li&gt;
&lt;li&gt;Do you like Twitter over Threads?
You are in.
Let's go.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Boom!!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function POST(req,res){
    const data  =await req.json()
    console.log(data)

    return NextResponse.json(data)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is it. Yoooooooooho.&lt;br&gt;
Hold on! What is this?&lt;br&gt;
Let me answer, this is how you handle POST request using &lt;code&gt;req.json()&lt;/code&gt;, and not &lt;code&gt;req.body&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's see how this works.&lt;/p&gt;

&lt;p&gt;Let's create a handleSubmit method in our &lt;code&gt;app/page.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const handleSubmit = async (e) =&amp;gt; {
    e.preventDefault()
    const submitData = {name,age}

    try {
      const res = await fetch('http://localhost:3000/api/handleform',{
        method: 'POST',
        body: JSON.stringify(submitData),
        headers: {
          'content-type': 'application/json'
        }
      })
      console.log(res)
      if(res.ok){
        console.log("Yeai!")
      }else{
        console.log("Oops! Something is wrong.")
      }
    } catch (error) {
        console.log(error)
    }
    setName('')
    setAge('')
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to pass this method in the form tag.&lt;br&gt;
&lt;code&gt;&amp;lt;form onSubmit={handleSubmit} ............. /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I hope I helped. Peace.&lt;br&gt;
Full code available &lt;a href="https://github.com/iambstha/blog-post-request-nextjs-app-router" rel="noopener noreferrer"&gt;here&lt;/a&gt; in GitHub.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>http</category>
      <category>api</category>
    </item>
    <item>
      <title>Next Auth for Stable App Router</title>
      <dc:creator>Bishal Shrestha</dc:creator>
      <pubDate>Fri, 07 Jul 2023 12:07:07 +0000</pubDate>
      <link>https://dev.to/iambstha/need-comment-on-features-add-494a</link>
      <guid>https://dev.to/iambstha/need-comment-on-features-add-494a</guid>
      <description>&lt;p&gt;Please go to the &lt;a href="https://next-auth-implementation-phi.vercel.app/" rel="noopener noreferrer"&gt;Live Demo&lt;/a&gt; deployed url &amp;amp; check if everything is working as expected. &lt;a href="https://github.com/iambstha/next-auth-implementation/issues" rel="noopener noreferrer"&gt;Create issue&lt;/a&gt; if any bug/issue found. Thank you in advance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7610adv76lsv39w2hipy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7610adv76lsv39w2hipy.jpg" alt=" " width="800" height="180"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/iambstha/next-auth-implementation" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;br&gt;
&lt;a href="https://next-auth-implementation-phi.vercel.app/" rel="noopener noreferrer"&gt;Live Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Detailed post on how to build this step by step is coming soon.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>nextauth</category>
    </item>
    <item>
      <title>Getting Started with Redux | React Tutorial</title>
      <dc:creator>Bishal Shrestha</dc:creator>
      <pubDate>Tue, 04 Jul 2023 17:29:09 +0000</pubDate>
      <link>https://dev.to/iambstha/getting-started-with-redux-react-tutorial-jcl</link>
      <guid>https://dev.to/iambstha/getting-started-with-redux-react-tutorial-jcl</guid>
      <description>&lt;h2&gt;
  
  
  What is Redux?
&lt;/h2&gt;

&lt;p&gt;Redux is a predictable state container for JavaScript applications, not limited to React, you see? It provides a centralized and predictable way to manage the state of an application, making it easier to track and update data as it flows through different components. Redux follows the principles of a Flux architecture, emphasizing a single source of truth and immutability. By maintaining a global store and using reducers to handle state changes, Redux helps simplify complex data flows, improve debugging capabilities, and facilitate efficient application development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning by building
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are we going to build?
&lt;/h3&gt;

&lt;p&gt;A simple counter app that displays the count value &amp;amp; a functionality to update the value of the count.&lt;/p&gt;

&lt;p&gt;We are going to learn how to access the state value stored by redux (where we will see how selector work in action) in the first section &amp;amp; then move on to some state updating section (where we will increase or decrease the value of count using a button where we will see dispatch in action).&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;JavaScript: A solid understanding of JavaScript fundamentals, including variables, functions, objects, arrays, and ES6 features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React: Familiarity with React basics, such as components, state, props, and lifecycle methods.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State Management: Understanding the basics of managing application state, including the concept of unidirectional data flow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flux Architecture: A high-level understanding of the Flux architecture pattern, which Redux is based on. (Not really required for this tutorial, but nice to have)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asynchronous JavaScript: Knowledge of asynchronous JavaScript concepts, such as Promises and async/await. (Not really required for this tutorial)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open command prompt &amp;amp; navigate to a directory where you want to create the project&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Starting a react app with create react app&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app counterapp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd counterapp&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now let’s install the redux packages required&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;npm install @reduxjs/toolkit&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install react-redux&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;That’s it. Now you are ready to use redux in your react app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just type npm start in command prompt&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Section I: Store, Slice, Accessing the state value
&lt;/h2&gt;

&lt;p&gt;Let’s start right away.&lt;/p&gt;

&lt;p&gt;I don’t know about you, but I like to clear the src folder altogether since most of them are not required.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the index.js &amp;amp; app.js file
&lt;/h3&gt;

&lt;p&gt;Create a file called index.js inside src folder &amp;amp; paste the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { Provider } from 'react-redux';
import store from './app/store';

ReactDOM.createRoot(document.getElementById('root')).render(
  &amp;lt;Provider store={store} &amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Provider&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: The Provider component wraps the App component, enabling access to the Redux store throughout the application. It takes a store prop, which should be the Redux store instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create another file called App.js inside src folder &amp;amp; insert&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Counter from "./components/Counter";

export default function App() {
  return (
      &amp;lt;Counter /&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here onward, things get a little bit tricky, if you are new to redux. Concepts like store, slice, selector &amp;amp; dispatch might confuse you. It takes time, you should really see how everything is working at each and every step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the store
&lt;/h3&gt;

&lt;p&gt;In Redux, the store is a core concept that serves as a centralized container for holding the application state. It is the single source of truth for the entire application's data. The store manages the state and provides methods to interact with and update it.&lt;/p&gt;

&lt;p&gt;Create a folder called app inside the src directory &amp;amp; inside the app directory, create a file called store.js &amp;amp; insert&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit'
import counterReducer from '../components/counterSlice'

export default configureStore({
    reducer: {
        counter : counterReducer
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: We are creating and exporting a Redux store using the configureStore function from the @reduxjs/toolkit library. The reducer ‘counterReducer’ is not exported yet from the slice, but we will get to that soon.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Creating a slice
&lt;/h3&gt;

&lt;p&gt;In Redux, a slice is a concept introduced by Redux Toolkit (a set of utilities for Redux) that helps organize and encapsulate related parts of the state, along with the corresponding reducers and actions.&lt;/p&gt;

&lt;p&gt;Create a folder called components inside the src directory &amp;amp; inside that, create a file called counterSlice.js &amp;amp; insert&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from '@reduxjs/toolkit'

export const counterSlice =  createSlice({
    name: 'counter',
    initialState: {
        value: 0
    }
})

export default counterSlice.reducer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: A slice requires a unique name in string in order to identify them. We can set the initial state value and reducers inside the slice, but here, we haven’t set any reducers which we will set soon while updating the count.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Creating our component to see the result
&lt;/h3&gt;

&lt;p&gt;Create another file called Counter.js inside the component folder &amp;amp; insert&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { useSelector } from 'react-redux'

const Counter = () =&amp;gt; {
    const count = useSelector(state =&amp;gt; state.counter.value)
  return (
    &amp;lt;div&amp;gt;
        &amp;lt;p&amp;gt;Count:  {count}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Counter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: The useSelector hook is used to select and extract the value property from the counter slice of the Redux store state. It subscribes the component to updates of that specific state slice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Count: 0&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Section II: Reducers &amp;amp; useDispatch
&lt;/h2&gt;

&lt;p&gt;Now that we have understood how to access the state value stored in the redux store, let’s move onto how to update the state with the use of reducers &amp;amp; useDispatch hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining the reducers
&lt;/h3&gt;

&lt;p&gt;Modify counterSlice.js to look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from '@reduxjs/toolkit'

export const counterSlice =  createSlice({
    name: 'counter',
    initialState: {
        value: 0
    },
    reducers: {
        increment: state =&amp;gt; {state.value += 1},
        decrement: state =&amp;gt; {state.value -= 1},
    }
})

export const {increment, decrement} = counterSlice.actions

export default counterSlice.reducer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: Here, we declared two reducers ‘increment’ &amp;amp; ‘decrement’ that simply returns the updated state value by increasing or decreasing the past state. Also, see that the reducers are exported as actions, because they are the actions that the useDispatch hook requires in order to update the state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using useDispatch() hook
&lt;/h3&gt;

&lt;p&gt;Also, modify the Counter.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { decrement, increment } from './counterSlice'
import './counter.css'
const Counter = () =&amp;gt; {
    const count = useSelector(state =&amp;gt; state.counter.value)
    const dispatch = useDispatch()
    return (
        &amp;lt;div className='box' &amp;gt;
            &amp;lt;p className='count'&amp;gt;&amp;lt;span&amp;gt;Count: &amp;lt;/span&amp;gt;{count}&amp;lt;/p&amp;gt;
            &amp;lt;div className=' btn ' &amp;gt;
                &amp;lt;button onClick={() =&amp;gt; dispatch(increment())} &amp;gt;Add&amp;lt;/button&amp;gt;
                &amp;lt;button onClick={() =&amp;gt; dispatch(decrement())} &amp;gt;Subtract&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Counter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: Here, we used useDispatch hook to handle the event. And also, I have put on some basic styles. All the code till now along with the style file can be found in my GitHub repo &lt;a href="https://github.com/iambstha/blog-project-redux-counterapp" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The result should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tvd5snafqp9fxeai4km.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3tvd5snafqp9fxeai4km.jpg" alt="Result of the code" width="577" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following this step-by-step guide, you should have gained a solid understanding of Redux concepts, including the store, slices, actions, reducers, and connecting Redux with React components. You should be able to apply this knowledge to build more complex Redux-powered applications and efficiently manage state across their projects.&lt;/p&gt;

&lt;p&gt;Remember, Redux is a powerful tool for state management, providing predictability, centralized state, and powerful developer tools. It may introduce some additional complexity initially, but once mastered, it can greatly enhance the maintainability and scalability of JavaScript applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://bishalshrestha.substack.com/p/getting-started-with-redux-react" rel="noopener noreferrer"&gt;Original post&lt;/a&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
