在R中将cURL转换为httr [英] Converting cURL to httr in R

查看:63
本文介绍了在R中将cURL转换为httr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆