Using Json string in the Http Header

后端 未结 4 1750
有刺的猬
有刺的猬 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);
    
    0 讨论(0)
  • 2020-12-30 21:36

    Yes, you may use JSON in HTTP headers, given some limitations.

    According to the HTTP spec, you MUST ensure that your header field-body contains only visible ASCII characters, tab, or space, and must not contain CR or LF characters (i.e. new lines, except via obsolete "folding whitespace").

    Since many JSON encoders (e.g. json_encode in PHP) will encode both CR and LF characters as "\r" and "\n", and encode invisible or non-ASCII characters (e.g. "é" becomes "\u00e9"), you often don't need to worry about this.

    Check the docs for your particular encoder or test it, though, because JSON strings technically allow most any UTF-8 character. For example, JSON.stringify() does not escape multibyte UTF-8, by default. However, you can easily modify it to do so, e.g.

    var charsToEncode = /[\u007f-\uffff]/g;
    function http_header_safe_json(v) {
      return JSON.stringify(v).replace(charsToEncode,
        function(c) {
          return '\\u'+('000'+c.charCodeAt(0).toString(16)).slice(-4);
        }
      );
    }
    

    Source

    Alternatively, you can do as @rocketspacer suggested and base64-encode the JSON before inserting it into the header field (e.g. how JWT does it). This makes the JSON unreadable (by humans) in the header, but ensures that it will conform to the spec.


    Worth noting, the original ARPA spec (RFC 822) has a special description of this exact use case, and the spirit of this echoes in later specs such as RFC 7230:

    Certain field-bodies of headers may be interpreted according to an internal syntax that some systems may wish to parse.

    Also, RFC 822 and RFC 7230 explicitly give no length constraints:

    HTTP does not place a predefined limit on the length of each header field or on the length of the header section as a whole, as described in Section 2.5.

    0 讨论(0)
  • 2020-12-30 21:36

    Generally speaking you do not send data in the header for a REST API. If you need to send a lot of data it best to use an HTTP POST and send the data in the body of the request. But it looks like you are trying to pass credentials in the header, which some REST API's do use. Here is an example for passing the credentials in a REST API for a service called SMSIfied, which allows you to send SMS text message via the Internet. This example is using basic authentication, which is a a common technique for REST API's. But you will need to use SSL with this technique to make it secure. Here is an example on how to implement basic authentication with WCF and REST.

    0 讨论(0)
  • 2020-12-30 21:44

    From what I understand using a json string in the header option is not as much of an abuse of usage as using http DELETE for http GET, thus there has even been proposal to use json in http header. Of course more thorough insights are still welcome and the accepted answer is still to be given.

    0 讨论(0)
提交回复
热议问题