在 R 中将 cURL 转换为 httr [英] Converting cURL to httr in R
问题描述
在与 Zendesk API 通信时,我无法将 cURL 请求转换为 httr 发布请求.我已经成功地从 API 中提取了数据,但到目前为止发布证明是有问题的.
I'm having trouble in converting a cURL request, into a httr post request, when communicating with the Zendesk API. I've successfully pulled data from the API, but posting is so far proving problematic.
我已经与 Zendesk 的 API 支持人员进行了交谈,但不幸的是,他们没有任何 R 方面的经验,因此无法告诉我我的脚本是否有误.
I've spoken to Zendesk's API support staff, but unfortunately, they don't have any experience in R, so can't tell me if my script is wrong or not.
我给出的 cURL 示例如下(所有敏感信息已替换):
The cURL example I've been given, is as follows (all sensitive information replaced):
curl https://{subdomain}.zendesk.com/api/v2/nps/surveys/{survey_id}/invitations.json -d '{"invitation": {"recipients": [{"name": "Ed C", "email": "example@subdomain.com", "language": "en-US"}]}}'
-H "Content-Type: application/json"
-v -u {your_email}/token:{your_api_token} -X POST
我的 cURL 知识非常有限,但我相信我使用以下脚本正确联系了 API(同样,所有敏感信息都已替换):
My cURL knowledge is very limited, but I believe I am contacting the API correctly with the below script (again, all sensitive information replaced):
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": "example@subdomain.com", "language": "en-US"}]}}'
,encode='json'
)
我反复搜索 Stack Overflow 以及其他来源,但到目前为止还没有找到适用于我的问题的情况.
I've scoured Stack Overflow repeatedly, as well as other sources, but haven't found a situation applicable to my issue so far.
提前致谢.
推荐答案
经过多次反复试验,我设法通过添加 'content_type_json()'
解决了我的问题.我不是 100% 确定为什么这确实有效,所以如果有人想澄清,我很感激输入.
After much trial and error, I managed to resolve my issue by adding in 'content_type_json()'
. I'm not 100% sure why exactly this worked, so if someone would like to clarify, I'd appreciate the input.
请参阅下面的完整代码:
Please see below for full code:
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": "example@subdomain.com", "language": "en-US"}]}}'
,encode='json'
)
这篇关于在 R 中将 cURL 转换为 httr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!