gpt4 book ai didi

ruby-on-rails - 用于 HTTP 请求 header 值的文本编码是什么?

转载 作者:可可西里 更新时间:2023-11-01 16:34:39 24 4
gpt4 key购买 nike

我有一个 Ruby on Rails 应用程序,它是 Java 和 .Net 应用程序的服务器。我有一个用于发送一些数据的自定义 header ,但是当此数据到达 Ruby on Rails 应用程序时,Rails 将值读取为 UTF-8,然后说该值不是有效的 UTF- 8串。

例如,如果我发送 JÜRGENELITE-HP 我会得到:

#<ActiveRecord::StatementInvalid: PGError: ERROR:  invalid byte sequence for encoding "UTF8": 0xdc52
: SELECT * FROM "replicas" WHERE ("replicas"."identification" = 'J?RGENELITE-HP') AND ("replicas".user_id = 121) LIMIT 1>

Java HTTP 客户端库在控制台中清楚地正确打印数据:

DEBUG [main] (DefaultClientConnection.java:268) - >> POST /ze/api/files.json HTTP/1.1
DEBUG [main] (DefaultClientConnection.java:271) - >> X-Replica: JÜRGENELITE-HP
DEBUG [main] (DefaultClientConnection.java:271) - >> Authorization: Basic bWxpbmhhcmVzOjEyMzQ1Njc4

DEBUG [main] (DefaultClientConnection.java:271) - >> Content-Length: 0
DEBUG [main] (DefaultClientConnection.java:271) - >> Host: localhost:3000
DEBUG [main] (DefaultClientConnection.java:271) - >> Connection: Keep-Alive
DEBUG [main] (DefaultClientConnection.java:271) - >> User-Agent: Apache-HttpClient/4.1.2 (java 1.5)

但是当它到达 Rails 时它就崩溃了。 HTTP 使用什么编码来编码 header 值?

最佳答案

US-ASCII

如果您查看 RFC2616 的第 2.2 节:

2.2 Basic Rules

The following rules are used throughout this specification to
describe basic parsing constructs. The US-ASCII coded character set
is defined by ANSI X3.4-1986 [21].

   OCTET          = <any 8-bit sequence of data>
CHAR = <any US-ASCII character (octets 0 - 127)>
UPALPHA = <any US-ASCII uppercase letter "A".."Z">
LOALPHA = <any US-ASCII lowercase letter "a".."z">
ALPHA = UPALPHA | LOALPHA
DIGIT = <any US-ASCII digit "0".."9">
CTL = <any US-ASCII control character
(octets 0 - 31) and DEL (127)>
CR = <US-ASCII CR, carriage return (13)>
LF = <US-ASCII LF, linefeed (10)>
SP = <US-ASCII SP, space (32)>
HT = <US-ASCII HT, horizontal-tab (9)>
<"> = <US-ASCII double-quote mark (34)>

本节的其余部分包含有关 header 和协议(protocol)其他元素的更多具体信息。

您必须在规范中跳来跳去才能找到所有正确的 BNF 定义。不过,第 4.2 节包含 header 的定义:

   message-header = field-name ":" [ field-value ]
field-name = token
field-value = *( field-content | LWS )
field-content = <the OCTETs making up the field-value
and consisting of either *TEXT or combinations
of token, separators, and quoted-string>

TEXT 在 2.2 节中定义:

   TEXT           = <any OCTET except CTLs,
but including LWS>

关于ruby-on-rails - 用于 HTTP 请求 header 值的文本编码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10356216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com