R POST API 请求与 httr returns 错误请求
R POST API request with httr returns bad requests
想用 curl
和 R
中的 httr
包实现以下 POST
请求
curl 'https://api.openfigi.com/v2/mapping' \
--request POST \
--header 'Content-Type: application/json' \
--data '[{"idType":"ID_WERTPAPIER","idValue":"851399","exchCode":"US"}]'
基于此Git issue我试过:
library(jsonlite)
library(rjson)
#>
#> Attaching package: 'rjson'
#> The following objects are masked from 'package:jsonlite':
#>
#> fromJSON, toJSON
library(httr)
url = 'https://api.openfigi.com/v2/mapping'
body1 = list(
idType = jsonlite::unbox('ID_WERTPAPIER'),
idValue = jsonlite::unbox('851399'),
exchCode = jsonlite::unbox('US')
)
r = httr::POST(url, body = body1, encode = 'json', verbose())
body2 = rjson::toJSON(list(
idType = "ID_WERTPAPIER",
idValue = "851399",
exchCode = "US"
))
r = httr::POST(url, body = body2, encode = "form", verbose())
由 reprex package (v0.3.0)
于 2019-08-26 创建
但都是错误的请求。
如果您只翻译 curl,则以下内容似乎有效。够了吗?
require(httr)
headers = c(
`Content-Type` = 'application/json'
)
data = '[{"idType":"ID_WERTPAPIER","idValue":"851399","exchCode":"US"}]'
r <- httr::POST(url = 'https://api.openfigi.com/v2/mapping', httr::add_headers(.headers=headers), body = data)
print(r$status_code)
想用 curl
和 R
httr
包实现以下 POST
请求
curl 'https://api.openfigi.com/v2/mapping' \
--request POST \
--header 'Content-Type: application/json' \
--data '[{"idType":"ID_WERTPAPIER","idValue":"851399","exchCode":"US"}]'
基于此Git issue我试过:
library(jsonlite)
library(rjson)
#>
#> Attaching package: 'rjson'
#> The following objects are masked from 'package:jsonlite':
#>
#> fromJSON, toJSON
library(httr)
url = 'https://api.openfigi.com/v2/mapping'
body1 = list(
idType = jsonlite::unbox('ID_WERTPAPIER'),
idValue = jsonlite::unbox('851399'),
exchCode = jsonlite::unbox('US')
)
r = httr::POST(url, body = body1, encode = 'json', verbose())
body2 = rjson::toJSON(list(
idType = "ID_WERTPAPIER",
idValue = "851399",
exchCode = "US"
))
r = httr::POST(url, body = body2, encode = "form", verbose())
由 reprex package (v0.3.0)
于 2019-08-26 创建但都是错误的请求。
如果您只翻译 curl,则以下内容似乎有效。够了吗?
require(httr)
headers = c(
`Content-Type` = 'application/json'
)
data = '[{"idType":"ID_WERTPAPIER","idValue":"851399","exchCode":"US"}]'
r <- httr::POST(url = 'https://api.openfigi.com/v2/mapping', httr::add_headers(.headers=headers), body = data)
print(r$status_code)