using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Dynamic; using System.Net; using System.Text.RegularExpressions; using Umbraco.Core.Logging; using Umbraco.Forms.Core; using Umbraco.Forms.Core.Attributes; using Umbraco.Forms.Core.Enums; public class PostToUrlAsJson : WorkflowType { [Setting("Url", description = "Enter the url to post to", view = "TextField")] public string Url { get; set; } [Setting("Bearer Token", description = "Optional: enter a bearer token for API access (don't include the word 'Bearer')", view = "TextField")] public string BearerToken { get; set; } public PostToUrlAsJson() { this.Id = new Guid("eb5d0597-4964-43c4-9437-407cba35dfc6"); this.Name = "Send form to URL as JSON object"; this.Description = "Sends the form to a url"; this.Icon = "icon-terminal"; this.Group = "Services"; } public override List ValidateSettings() { List list = new List(); if (string.IsNullOrEmpty(Url)) { list.Add(new Exception("'Url' setting has not been set")); } return list; } public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e) { using (WebClient http = new WebClient()) { http.Headers.Add(HttpRequestHeader.ContentType, "application/json"); if (!string.IsNullOrEmpty(BearerToken)) { http.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + BearerToken); http.Encoding = System.Text.Encoding.UTF8; } var values = new Dictionary(); foreach (RecordField value in record.RecordFields.Values) { values.Add(value.Field.Alias, value.ValuesAsString(true)); } dynamic json = new ExpandoObject(); json.id = record.UniqueId; json.formId = e.Form.Id; json.formName = e.Form.Name; json.ip = record.IP; json.memberKey = record.MemberKey; json.time = DateTime.UtcNow; json.values = values; string payload = JsonConvert.SerializeObject(json); try { http.UploadData(Url, "POST", System.Text.Encoding.UTF8.GetBytes(payload)); return WorkflowExecutionStatus.Completed; } catch (Exception ex) { string err = String.Format("There was a problem sending the Record with unique id '{0}' from the Form '{1}' with id '{2}' to the URL Endpoint '{3}' with the method 'POST'", record.UniqueId, e.Form.Name, e.Form.Id, Url); LogHelper.Error(err, ex); return WorkflowExecutionStatus.Failed; } } } }