Skip to content

Instantly share code, notes, and snippets.

@akatakritos
Created December 7, 2015 21:12
Show Gist options
  • Save akatakritos/865257cd098e2bc268e3 to your computer and use it in GitHub Desktop.
Save akatakritos/865257cd098e2bc268e3 to your computer and use it in GitHub Desktop.
Kentico REST API Examples
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json.Linq;
namespace KenticoRestSample
{
public static class Program
{
const string username = "api";
const string password = "password";
const string rootUrl = "https://localhost/rest/";
static void Main(string[] args)
{
ChangeContactEmail();
Console.ReadKey();
}
private static void ChangeContactEmail()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(rootUrl);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", EncodeCredentials(username, password));
while (true)
{
var results = JObject.Parse(client.GetStringAsync("om.contact?format=json&topn=15&orderby=ContactID").Result);
foreach (var result in results["om_contacts"][0]["OM_Contact"])
{
Console.WriteLine($"{result["ContactID"]} : {result["ContactFirstName"]} {result["ContactLastName"]}");
}
Console.WriteLine("\r\nEdit which contact (type id, any non integer to exit): ");
int id;
if (!int.TryParse(Console.ReadLine(), out id))
return;
Console.WriteLine("Enter Name (John Smith): ");
var name = Console.ReadLine();
var firstName = name.Split(' ')[0];
var lastName = name.Split(' ')[1];
var patch = new JObject(
new JProperty("ContactFirstName", firstName),
new JProperty("ContactLastName", lastName));
var r = client.PutAsync($"om.contact/{id}?format=json", new StringContent(patch.ToString())).Result;
Console.WriteLine(r.StatusCode);
}
}
}
private static string EncodeCredentials(string username, string password)
{
var bytes = Encoding.UTF8.GetBytes($"{username}:{password}");
return Convert.ToBase64String(bytes);
}
}
}
@akatakritos
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment