Using Json string in the Http Header

后端 未结 4 1772
有刺的猬
有刺的猬 2020-12-30 21:14

Recently I run into some weird issue with http header usage ( Adding multiple custom http request headers mystery) To avoid the problem at that time, I have put the fields i

4条回答
  •  天涯浪人
    2020-12-30 21:28

    Base64encode it before sending. Just like how JSON Web Token do it.
    Here's a NodeJs Example:

    const myJsonStr = JSON.stringify(myData);
    const headerFriendlyStr = Buffer.from(myJsonStr, 'utf8').toString('base64');
    res.addHeader('foo', headerFriendlyStr);
    

    Decode it when you need reading:

    const myBase64Str = req.headers['foo'];
    const myJsonStr = Buffer.from(myBase64Str, 'base64').toString('utf8');
    const myData = JSON.parse(myJsonStr);
    

提交回复
热议问题