Skip to content

Setting request header or prefer c.get()? #2847

Closed Answered by 6km
exoup asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @exoup

It's possible to pass data using the following techniques (not just these)

Setting a custom request header

It can be done using the c.req.raw.headers.set() method but it should be before calling next().

Please notice that when you set a request header from the server side, it will not be passed to the client side. This technique is only used to pass data to the route handler.

app.use(async (c, next) => {
  c.req.raw.headers.set('x-username', '6km')
  await next()
})

app.get('/', (c) => {
  const passedData = c.req.header('x-username')
  // ...
})

Setting a custom response header

This technique is used to pass data to the server through response headers.

To set a response heade…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by exoup
Comment options

You must be logged in to vote
4 replies
@6km
Comment options

@exoup
Comment options

@6km
Comment options

@yusukebe
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants