Home > Software design >  How can i write a function to call(POST) an API, from within the web server and not the browser. -Ne
How can i write a function to call(POST) an API, from within the web server and not the browser. -Ne

Time:05-31

How can i write a function to call an API, from web server to API within the server itself. I do not want the browser to know that he is calling an API or and information about the API, even the link. The API call(POST method) should be executed on the server.

Using Nextjs with axios. I also want the function to be a component.

CodePudding user response:

in Next Js if you want to server side rendering you have to fetch data from API use getStaticProps;

CodePudding user response:

If you want to fetch data before loading the page (when the actural page load request happens), use server side rendering.

In Next.js you can use getServerSideProps for doing this. By doing this, you get to keep the api call inside the component, and the sever will fetch the data before rendering your component. Attaching a sample of code that you can refer for this.

export async function getServerSideProps(context) {
  return {
    props: {}, // will be passed to the page component as props
  }
}

For more info, please visit the nextjs official documents. Attaching a link for the above example.

https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props

  • Related