Skip to content

Consume API Routes from getStaticProps #3937

Answered by paulogdm
HaNdTriX asked this question in General
Discussion options

You must be logged in to vote

Yes it is, but you may need to do:

export async function getStaticProps(context) {
  // The following url must be absolute
  // but what is the correct origin/port? 
  const url = context.request.headers.host + '/api/post'

  return {
    props: await fetch(url).then(res => res.json())
  }
}

You need to access the "host" header in order to retrieve the information from where your request is coming from.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by coetry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants