r - 在 R 中将 cURL 转换为 httr

标签 r api curl httr zendesk

与 Zendesk API 通信时,我在将 cURL 请求转换为 httr post 请求时遇到问题。 我已成功从 API 中提取数据,但迄今为止发布数据存在问题。

我已经与 Zendesk 的 API 支持人员交谈过,但不幸的是,他们没有任何 R 经验,因此无法告诉我我的脚本是否错误。

我得到的 cURL 示例如下(所有敏感信息均已替换):

curl https://{subdomain}.zendesk.com/api/v2/nps/surveys/{survey_id}/invitations.json  -d '{"invitation": {"recipients": [{"name": "Ed C", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afcad7cec2dfc3caefdcdacdcbc0c2cec6c181ccc0c2" rel="noreferrer noopener nofollow">[email protected]</a>", "language": "en-US"}]}}' 
    -H "Content-Type: application/json" 
    -v -u {your_email}/token:{your_api_token} -X POST

我的 cURL 知识非常有限,但我相信我正在使用以下脚本正确联系 API(再次替换所有敏感信息):

r2 <- POST('https://{subdomain}.zendesk.com/api/v2/nps/surveys/{survery_id}/invitations.json'
          ,add_headers(Authorization="Basic {api_key}")
          ,body  ='{"invitation": {"recipients": [{"name": "Ed C", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67021f060a170b022714120503080a060e094904080a" rel="noreferrer noopener nofollow">[email protected]</a>", "language": "en-US"}]}}'
          ,encode='json'
)

我反复搜索了 Stack Overflow 以及其他来源,但到目前为止还没有找到适用于我的问题的情况。

提前致谢。

最佳答案

经过多次尝试和错误,我设法通过添加 'content_type_json()' 解决了我的问题。我不是 100% 确定为什么这会起作用,所以如果有人想澄清,我将不胜感激。

请参阅下面的完整代码:

r2 <- POST('https://{subdomain}.zendesk.com/api/v2/nps/surveys/{survery_id}/invitations.json'
              ,add_headers(Authorization="Basic {api_key}")
              ,content_type_json()
              ,body  ='{"invitation": {"recipients": [{"name": "Ed C", "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8feaf7eee2ffe3eacffcfaedebe0e2eee6e1a1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>", "language": "en-US"}]}}'
              ,encode='json'
    )

关于r - 在 R 中将 cURL 转换为 httr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51063443/

相关文章:

javascript - 通过应用程序登录谷歌帐户

javascript - 从 api url 打印 json 数据

C++:Libcurl curl_easy_init() 给出访问冲突错误并使程序崩溃

php - 如何在curl调用中访问Uber API OAuth 2.0不记名 token

python - 在 R 或 Python 中转换数据类型

r - 复杂的条件变异 : Create new variable with conditional mutating using only past observations for a given participant?

javascript - 在 Chrome 扩展程序的弹出文件中加载 YouTube 视频时出错?

java - elasticsearch 未启动 {0.90.0} : Startup Failed

r - 在 Shiny 中输入更改时切换到不同的 DataTable 页面?

按行 reshape 矩阵