I'm having trouble with sending a post request to a RESTful API. The API expects the following JSON body:
{ "APIKey":"String content", "ID":"String content", "Data":"String content", "TokenScheme":0 }
I'd like for it to use async but this is a first for me and I'm having issues just handling the return string. I'd just like to know how to send the string data to the view and I'll get it from there.
Here is what I'm trying to use but the browser just spins and spins.
public async Task<string> RunAsync()
{
using (var client = new HttpClient())
{
// TODO - Send HTTP requests
client.BaseAddress = new Uri("https://test-api.com/Services.svc/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string APIKey, ID, Data, Scheme;
// HTTP POST
string[] jsonRequest = { APIKey = "", ID = "", Data = "teststring", Scheme = "0" };
//alt option IHttpActionResult
HttpResponseMessage response = await client.PostAsJsonAsync("REST/Service", jsonRequest);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(@" successful.");
return "";
}
else
{
Debug.WriteLine(@" Failed.");
return "";
}
}
}