httr POST 身份验证错误

问题描述:

我正在尝试使用 httr 构建 POST json 请求.API 文档提出了以下 CURL 请求:

I am trying to structure a POST json request using httr. The API documentation proposes the following the CURL request:

curl -X POST -H "Authorization:Token XXXXXXXXX" -H "Content-Type: application/json" --data "{\"texts\":[\"A simple string\"]}" https://api.uclassify.com/v1/uclassify/topics/classify

我的 R httr 实现如下:

My R httr implementation is the following:

POST("https://api.uclassify.com/v1/uClassify/Topics/classify", 
    encode="json",
    add_headers('Authorization:Token'="XXXXXXXXX"),
    body=("A simple string"))

但我收到了 401 错误消息,表明我的身份验证失败.关于如何在 httr 上实现 CURL 请求的任何建议?

But I received a 401 error message which indicates that my authentication failed. Any suggestion on how I can implement the CURL request on httr?

以防万一它可以帮助别人,下面的代码对我有用:POST("https://api.uclassify.com/v1/uClassify/Topics/classify",编码=json",add_headers(Authorization = "Token XXXXXXXXX"),body = "{\"texts\":[\"一个简单的字符串\"]}")

Just in case it can help somebody else, the below code works for me: POST("https://api.uclassify.com/v1/uClassify/Topics/classify", encode="json", add_headers(Authorization = "Token XXXXXXXXX"), body = "{\"texts\":[\"A simple string\"]}")