在字符串中嵌入nul,以导入Content-Type的原始数据:text / tab-separated-values;字符集= utf-16le

时间:2018-09-21 16:32:56

标签: r http oauth utf-16 httr

使用httr使用oath2.0从站点获取报告,我无法将原始内容转换为R中的字符集。

 > req <-GET("https://www.blah.com/blah/v2/blah", config(token = token))

我的回复表示没有问题:

 Response [https://www.blah.com/blah/v2/blah]
 Date: 2018-09-21 15:55
 Status: 200
 Content-Type: text/tab-separated-values; charset=utf-16le
 Size: 21.1 MB
NA

当尝试将原始数据转换为char时,我得到:

> rawToChar(req$content)
Error in rawToChar(req$content) : 
embedded nul in string:

通过content()检查内容时,我还会遇到以下错误:

> content(req)
Error in guess_header_(datasource, tokenizer, locale) :
Incomplete multibyte sequence

有什么想法吗?我在网络上发现了有限的资源...

2 个答案:

答案 0 :(得分:0)

供参考。对于原始结构,“ 00”表示NUL。 解决方案是删除所有NUL值,然后转换为char。

 > dat <- req$content
 > up_dat <- dat[!dat=='00']
 > rawToChar(up_dat)

一旦转换,删除对整体数据结构没有影响。

在这种情况下

  readr::read_tsv()

工作正常。

答案 1 :(得分:0)

您还可以使用readBin()来读取原始向量。唯一需要知道或猜测的是n使用的大小。但是您可以通过计算NUL值来进行计数。

count_nul <- length(dat[dat == 00])
readBin(dat, n = count_nul)