如何在 R 中调用 API Post
请求 URL https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment
请求头 Ocp-Apim-Subscription-Key = 一些值 & Content-Type = application/json
正文应用程序/json
{
"documents": [
{
"language": "string",
"id": "string",
"text": "string"
}
]
}
Run Code Online (Sandbox Code Playgroud)
请帮忙!!!
小智 10
这是例子——
request_body <- data.frame(
language = c("en","en"),
id = c("1","2"),
text = c("This is wasted! I'm angry","This is awesome! Good Job Team! appreciated")
)
Run Code Online (Sandbox Code Playgroud)
require(jsonlite)
request_body_json <- toJSON(list(documents = request_body), auto_unbox = TRUE)
Run Code Online (Sandbox Code Playgroud)
require(httr)
result <- POST("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment",
body = request_body_json,
add_headers(.headers = c("Content-Type"="application/json","Ocp-Apim-Subscription-Key"="my_subscrition_key")))
Output <- content(result)
Run Code Online (Sandbox Code Playgroud)
输出