Created
July 19, 2017 04:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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