使用Curl进行POST请求到WebApi时出现字符编码问题

4
我们有一个WebApi服务,接受POST请求。POST请求中的信息包含这个Unicode字符 - ¡ (http://www.fileformat.info/info/unicode/char/a1/index.htm)。
当我们使用Fiddler测试服务时,服务器正确地接收到了信息。但是在使用curl进行相同信息的POST时,该字符会被更改为来自特殊块的另一个字符 - http://www.fileformat.info/info/unicode/char/fffd/index.htm
您有关于为什么会发生这种情况以及如何使Curl请求正确地编码所需内容的任何想法都将不胜感激。
编辑: curl请求
curl --trace-ascii debug.txt -d "=VALT ¡ OFFER = '111501383'" http://localhost:25980/api/uni

Api控制器代码

// POST api/<controller>
public string Post([FromBody]string value)
{
// code to process the value
}

Fiddler请求(使用Composer选项卡)

POST http://localhost:25980/api/uni HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-IN,en;q=0.5
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Content-Type: application/x-www-form-urlencoded; charset=US-ASCII
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 47
Host: localhost:25980

"=VALT ¡ OFFER = '111501383'"

请添加您发送请求的curl代码。另外,ApiContoller的代码也会很有帮助。 - Nikolai Samteladze
更新了带有代码的问题。 - jbagavathi
这是在Windows上的curl,对于命令行中的非ASCII字符可能会有些棘手。这是一个Unicode (/U)控制台吗? - bzlm
1个回答

4

这是一些建议。我将在今天晚些时候尝试复制您的问题,并更新答案。

内容类型:application/x-www-form-urlencoded; charset=US-ASCII

此行指定您的数据使用 US-ACSII 编码方案进行编码,该方案不包括 ¡ 字符。请尝试使用以下选项将其设置为 UTF-8

-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8"

我同意你的建议,但看起来他们“试图”发送的字符实际上是ASCII 0xa1,这可能会让curl感到困惑。我猜他们实际上发送的是UTF-8 0xc2a1。 - bzlm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接