Skip to content

Instantly share code, notes, and snippets.

@miguelerm
Created July 19, 2017 04:43
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 miguelerm/3062f99ff289e0b8773ec9326c27bab8 to your computer and use it in GitHub Desktop.
Save miguelerm/3062f99ff289e0b8773ec9326c27bab8 to your computer and use it in GitHub Desktop.
C# demo to consume LimeSurvey API based on https://manual.limesurvey.org/RemoteControl_2_API#NodeJS_example
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
namespace LimeSurveyApiTest
{
class Program
{
private static readonly HttpClient client = new HttpClient();
static void Main(string[] args)
{
var url = "http://PATH_OF_YOUR_SERVER/index.php/admin/remotecontrol";
var getSessionKeyJson = JsonConvert.SerializeObject(new { method = "get_session_key", @params = new[] { "YOUR_USERNAME", "YOUR_PASSWORD" }, id = 1 });
var getSessionKeyContent = new StringContent(getSessionKeyJson, Encoding.UTF8, "application/json");
var getSessionKeyResponse = client.PostAsync(url, getSessionKeyContent).Result;
if (getSessionKeyResponse.IsSuccessStatusCode)
{
var getSessioneyResult = getSessionKeyResponse.Content.ReadAsStringAsync().Result;
var sessionKey = JsonConvert.DeserializeAnonymousType(getSessioneyResult, new { result = "" }).result;
var getSurveyJson = JsonConvert.SerializeObject(new { method = "list_surveys", @params = new[] { sessionKey }, id = 1 });
var getSurveyContent = new StringContent(getSurveyJson, Encoding.UTF8, "application/json");
var getSurveyResponse = client.PostAsync(url, getSurveyContent).Result;
var surveys = getSurveyResponse.Content.ReadAsStringAsync().Result;
if (getSurveyResponse.IsSuccessStatusCode)
{
Console.WriteLine($"Response: {surveys}");
}
else
{
Console.WriteLine($"List Surveys: {getSurveyResponse.StatusCode}");
}
}
else
{
Console.WriteLine($"Get Session Key: {getSessionKeyResponse.StatusCode}");
}
Console.WriteLine("End.");
Console.ReadLine();
}
}
}
@Abdel-A
Copy link

Abdel-A commented Mar 20, 2019

Hi miguelerm,
When I execute this line : "var sessionKey = JsonConvert.DeserializeAnonymousType(getSessioneyResult, new { result = "" }).result;", I receive this error:
HResult=-2146233088
LineNumber=0
LinePosition=0
Message=Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Path=""
Source=Newtonsoft.Json

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