I hope that someone can help with an actual example
From various posts I have gotten this far, but I keep getting Bad Request.
Basically what i'm trying to achieve is to login using a AppToken key and then retrieve the SessionToken from the Response. This is the info they posted from their API documentation
- Request (JSON)
- POST /login/application
- Content-Type: application/json
- {"Name":"micros", "Key":"longstringasyourpasscodegeneratedfromtheapplication"}
The response looks like this (when using Simple REST Client)
<Login xmlns="http://schemas.datacontract.org/2004/07/Tamigo.Services.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DefaultCompanyId i:nil="true"/><DefaultCompanyName i:nil="true"/><DefaultDepartmentId i:nil="true"/><DefaultDepartmentName/><Email/><EmployeeId i:nil="true"/><ImageUrl i:nil="true"/><MenuId>0</MenuId><Name i:nil="true"/><Password/><Role>Application</Role><SessionToken>e1f35353-08f7-4213-a6b9-251313b36701</SessionToken></Login>
And from that Response I need to somehow get the "SessionToken" into a variable to use in the next GET Request.
This is the code I put together so far, which is pieces of code that I could find from other similar questions (which of course didn't fit nicely into my apparently completely unique/never seen before type of request):
using System;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;
namespace RESTServicesXMLParseExample
{
class Program
{
static void Main(string[] args)
{
try
{
var request =
(HttpWebRequest)WebRequest.Create("https://api.tamigo.com/login/application");
var postData = "Name=test";
postData += "&Key=y3LIZ5u5yR9A7a98ypBdygQyIBrqQwZdfdfZKmgtErQ=";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//Create the REST Services 'Find by Query' request
XmlDocument locationsResponse = MakeRequest(responseString);
ProcessResponse(locationsResponse);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
}
//Submit the HTTP Request and return the XML response
public static XmlDocument MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
return (xmlDoc);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
return null;
}
}
static public void ProcessResponse(XmlDocument locationsResponse)
{
//Create namespace manager
XmlNamespaceManager nsmgr = new XmlNamespaceManager(locationsResponse.NameTable);
nsmgr.AddNamespace("rest", "http://schemas.datacontract.org/2004/07/Tamigo.Services.Entities");
XmlNodeList formattedAddressElements = locationsResponse.SelectNodes("//rest:SessionToken", nsmgr);
Console.WriteLine("SessionToken");
foreach (XmlNode SessionToken in formattedAddressElements)
{
Console.WriteLine(SessionToken.InnerText);
}
Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}
If your API documentation tells you to send an application/json payload, then you better not ignore that as the content-type
application/x-www-form-urlencoded
is really something different.With the below changes I get an 401 Unauthorized because I hope the key and/or name are invalid. So I resolved the Bad Request thing.
Here is the small helper class that I used so the DataContractJsonSerializer could do its job: