将 csv 文件导入为 api 查询

Importing csv file as an api query

所以我试图调用“Content-Type:text/csv”API,然后在 r.

中将其转换为可读格式

调用成功,但是管理返回的对象是当前的挑战。

虽然,我看过只有 JSON 对象的例子,我相信管理这个内容类型应该更容易处理,但这是我第一次直接 API 调用。

这是我尝试过的:

library(tidyverse)
library(rjson)
library(jsonlite)
library(RCurl)
library(httr)

## Export Data URL
url = 'http://api.abc.com/api/users'

headers = c('accept' = {'application/json'},
            'Accept-Language' = {'hi_IN'},
            'User-Agent' = {'Mozilla/4.0'})

## Raw Data
api_raw <- GET(url, add_headers(headers))
api_raw

正如 Shah 指出的那样,我已经能够隔离文本响应,但是现在的挑战是将此 数据 转换为可读形式。

我试过以下方法:

setwd("~/Automated Reports/R/BME-Export")
data <- httr::content(api_raw, as = 'text')

data_2 <- jsonlite::fromJSON(data)

这里我得到这个错误

Error: parse error: unallowed token at this point in JSON text ,Lat,Long,Popu (right here) ------^ In addition: Warning messages: 1: package ‘RCurl’ was built under R version 4.1.1 2: package ‘httr’ was built under R version 4.1.1

## Read the data as csv
df_api <- read.csv(data)

这里出现如下错误

Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file

尝试使用 httr::content 函数。

data <- httr::content(api_raw, 'text')

这是我能得到的最好的:

data <- httr::GET(url) %>% httr::content("parsed")