简体   繁体   中英

How to add header to getServerSideProps in Next.js

I want to pass JWT access token with a getServersideProps in Next.js. The token is stored in local storage. Is there any way to do it?

Want to add headers to getServerSideProps in Next.js

getServerSideProps accepts a context parameter which includes keys for accessing the IncomingMessage ( req ) and ServerResponse (res ) objects. If you want to add a header, you can use the response object's setHeader method.

export function getServerSideProps(context) {
  // print incoming headers
  console.log(context.req.headers);

  // add header
  context.res.setHeader("X-Foo", "Bar");

  return {
    props: {},
  };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM