Skip to content

Instantly share code, notes, and snippets.

@StefanRiedmann
Created October 3, 2017 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanRiedmann/83e014dd480c5a4cadc181e87738a6b3 to your computer and use it in GitHub Desktop.
Save StefanRiedmann/83e014dd480c5a4cadc181e87738a6b3 to your computer and use it in GitHub Desktop.
Test for creating an Infusionsoft Resthook
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using ServiceStack;
using Xunit;
namespace Tests
{
public class InfusionsoftHookRequest
{
[JsonProperty("eventKey")]
public string EventKey { get; set; }
[JsonProperty("hookUrl")]
public string HookUrl { get; set; }
}
public class Temp
{
private string _accessToken = "abc";
private string _infusionsoftUrl = "https://api.infusionsoft.com/crm/rest/v1/hooks";
private string _callbackUrl = "http://www.mypage.com/api/infusionsoft/hookcallback";
[Fact]
public void CreateWebhookTest()
{
var hookObj = new InfusionsoftHookRequest
{
EventKey = "contact.edit",
HookUrl = _callbackUrl
//HookUrl = HttpUtility.UrlEncode(_callbackUrl) //I tried both ways
};
var data = JsonConvert.SerializeObject(hookObj);
var request = (HttpWebRequest)WebRequest.Create(_infusionsoftUrl);
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "application/json, */*"; //I tried both ways
request.Accept = "application/json, */*";
var dataBytes = Encoding.UTF8.GetBytes(data);
using (var reqStream = request.GetRequestStream())
{
reqStream.Write(dataBytes, 0, dataBytes.Length);
}
request.Headers[HttpRequestHeader.Authorization] = $"Bearer {_accessToken}";
string resultJson;
try
{
using (var response = request.GetResponse())
{
var sr = new StreamReader(response.GetResponseStream());
resultJson = response.ReadToEnd();
sr.Close();
}
}
catch (Exception e)
{
Debug.WriteLine($"Error creating webhook: {e.Message}");
return;
}
Debug.WriteLine($"CreateWebhook result: {resultJson}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment