英文:
send data with special characters via POST request in R using httr package
问题
我正在使用httr包发送POST请求来发送数据。发送的数据(json)已经编码为UTF-8,但接收到的数据没有正确的编码,特殊字符被错误地解读。以下是我用来发送POST请求的代码:
future({
POST(
url=url,
body=pbody,
config = httr::config(ssl_verifypeer = FALSE),
add_headers(.headers=c("Content-Type"="application/json")),
encode="json"
)
})
我将感激任何帮助。提前谢谢。
英文:
I am sending data using POST request with httr package. The data sent (json) has the special character encoded (UTF-8) but the data received doesn't have proper encoding and special characters are being misread. Below is the code I am using to send the POST request:
future({
POST(
url=url,
body=pbody,
config = httr::config(ssl_verifypeer = FALSE),
add_headers(.headers=c("Content-Type"="application/json")),
encode="json"
)
})
I will appreciate any help. Thank you in advance.
答案1
得分: 2
以下是您要翻译的部分:
这并不清楚你说的数据是什么意思,当你说你的数据是 JSON 格式时。我假设你的意思是它是一个包含特殊字符的 JSON 字符串,例如:
pbody <- '{ "greeting":["हैलो वर्ल्ड"]}'
pbody
#> [1] "{\"greeting\":[\"हैलो वर्ल्ड\"]}"
如果是这种情况,将消息发送到服务器应该没有问题。如果我们使用 httpbin 服务,那么我们可以确切地看到消息在服务器上的到达情况,因为它将不经修改地发送回给我们。
请注意,如果我们有一个 JSON 字符串,我们应该将其发送为原始数据。encode = 'json'
的存在是为了将 R 对象转换为 JSON。
library(httr)
url <- "https://httpbin.org/post"
response <- POST(url, body = pbody, encode = 'raw')
响应如下:
response
#> Response [https://httpbin.org/post]
#> Date: 2023-07-13 08:16
#> Status: 200
#> Content-Type: application/json
#> Size: 638 B
#> {
#> "args": {},
#> "data": "{\"greeting\":[\"\u0939\u0948\u0932\u094b...
#> "files": {},
#> "form": {},
#> "headers": {
#> "Accept": "application/json, text/xml, application/xml, */*",
#> "Accept-Encoding": "deflate, gzip",
#> "Content-Length": "48",
#> "Host": "httpbin.org",
#> ...
快速查看响应使我们认为可能存在问题,因为我们在 data
字段中的特殊字符似乎已经消失,并被替换为 [\"\u0939\u0948\u0932\u094b...
。但是,这只是在这种情况下打印一些 Unicode 字符的方式。如果我们解析响应,我们将看到我们的特殊字符仍然存在并被识别为特殊字符:
content(response, 'parsed')$json
#> greeting
#> $greeting[[1]]
#> [1] "हैलो वर्ल्ड"
事实上,我们可以获得我们的初始字符串,将其发送到服务器并再次返回:
content(response, 'parsed')$json |> jsonlite::toJSON() |> as.character()
#> [1] "{\"greeting\":[\"हैलो वर्ल्ड\"]}"
英文:
It's not clear what you mean when you say that your data is in json format. I assume you mean it is a character string of json with special characters, such as:
pbody <- '{"greeting":["हैलो वर्ल्ड"]}'
pbody
#> [1] "{\"greeting\":[\"हैलो वर्ल्ड\"]}"
If this is the case, there should be no problem sending your message to the server. If we use the httpbin service, then we can see exactly what message arrives on the server, since it will be sent back to us unaltered.
Note that if we have a json string, we should send it as raw. The encode = 'json'
exists to convert R objects into json.
library(httr)
url <- "https://httpbin.org/post"
response <- POST(url, body = pbody, encode = 'raw')
The response looks like this:
response
#> Response [https://httpbin.org/post]
#> Date: 2023-07-13 08:16
#> Status: 200
#> Content-Type: application/json
#> Size: 638 B
#> {
#> "args": {},
#> "data": "{\"greeting\":[\"\u0939\u0948\u0932\u094b \u0935\u0930\u094d...
#> "files": {},
#> "form": {},
#> "headers": {
#> "Accept": "application/json, text/xml, application/xml, */*",
#> "Accept-Encoding": "deflate, gzip",
#> "Content-Length": "48",
#> "Host": "httpbin.org",
#> ...
A quick look at the response makes us think that there has been a problem, since our special characters in the data
field seem to have gone and been replaced with [\"\u0939\u0948\u0932\u094b...
. However, this is just the way that some Unicode characters are printed in this context. If we parse the response, we will see that our special characters are still there and recognized as such:
content(response, 'parsed')$json
#> greeting
#> $greeting[[1]]
#> [1] "हैलो वर्ल्ड"
In fact we can get our initial string back, having had it being sent to the server and back again:
content(response, 'parsed')$json |> jsonlite::toJSON() |> as.character()
#> [1] "{\"greeting\":[\"हैलो वर्ल्ड\"]}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论