Initating a Trustpilot invitation using Trustpilot API on Coldfusion

298 Views Asked by At

I'm trying to write a web application to send automated Trustpilot invitations, however, when trying to submit the data in the 2nd CFHTTP to process the invition every "type" option in the CFHTTPPARAM returns a "Unsupported media type" error:

<cfset apiKey = "XXXXXXX">
<cfset secret = "YYYYY">

<cfset encAccess = apiKey & ":" & secret>
<cfset encAccess = ToBase64(encAccess)>
    
<cfset myurl = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken">

<!---Generate Access Token Call--->
<cfhttp url="#myurl#" result="myauth" method="POST">
  <cfhttpparam type="HEADER" value="Basic[#encAccess#]" name="Authorization"></cfhttpparam>
  <cfhttpparam type="HEADER" value="application/x-www-form-urlencoded" name="Content-Type"></cfhttpparam>
  <cfhttpparam type="FORMFIELD" value="password" name="grant_type"></cfhttpparam>
  <cfhttpparam type="FORMFIELD" value="[email protected]" name="username"></cfhttpparam>
  <cfhttpparam type="FORMFIELD" value="Password" name="password"></cfhttpparam>              
</cfhttp>


<cfset result = DeserializeJSON(#myauth.fileContent#)>
<cfset token = #result.access_token#>   


<!--SET DATA TO SEND FOR INVITION--->
<cfset mydata = "{
'consumerEmail': '[email protected]',
'replyTo': '[email protected]',
'referenceNumber': '11223344',
'consumerName': 'Consumer Name',
'locale': 'en-US',
'senderEmail': '[email protected]',
'serviceReviewInvitation': {
  'preferredSendTime': '2020-09-01T17:00:00', 
  'redirectUri': 'https://uk.trustpilot.com',
   ],
  'templateId': 'XXXYYYZZZ'
 },
'senderName': 'MyCompany'
}">

<cfset inviteurl = "https://invitations-api.trustpilot.com/v1/private/business-units/X1X1X1X1X1X1/email-invitations?token=" & token>

<cfhttp url="#inviteurl#" result="newresult" method="POST">
   <cfhttpparam value="#mydata#" name="request" type="FORMFIELD"></cfhttpparam>
</cfhttp>

<cfdump var="#newresult#">

For reference, I'm trying to follow the guidelines set out here: https://developers.trustpilot.com/invitation-api#create-invitation(s)

Any help on the later half of this code would be a great help.

1

There are 1 best solutions below

0
On

I believe Trustpilot's API are expecting the JSON data as the request body, but you are trying to stuff it inside a form field. Try:

<cfhttp url="#inviteurl#" result="newresult" method="POST">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#mydata#" />
</cfhttp>

Note that the above assumes that mydata is already a string as in your example. If it was still structured data then you would use <cfhttpparam type="body" value="#serializeJson(mydata)#" /> instead.