2

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.

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.

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

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'
)

I've scoured Stack Overflow repeatedly, as well as other sources, but haven't found a situation applicable to my issue so far.

Thanks in advance.

Ed Cunningham
  • 179
  • 1
  • 3
  • 17

1 Answers1

2

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'
    )
Ed Cunningham
  • 179
  • 1
  • 3
  • 17