Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created March 29, 2017 09:35
Show Gist options
  • Save ahelland/28aed86728831d52299501a7e2be32c2 to your computer and use it in GitHub Desktop.
Save ahelland/28aed86728831d52299501a7e2be32c2 to your computer and use it in GitHub Desktop.
API Playground - C#
[HttpPost]
public async Task<IActionResult> CSharp(Code code)
{
string output = code.csx;
var apiURL = "https://www.microsoft.com/net/api/code";
using (var client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(apiURL));
request.Headers.Add("Referer", "https://www.microsoft.com/net");
var codeSnippet = code.csx;
var codeInput = new CodeRequest { language = "csharp", captureStats = false, sources = new string[1] };
codeInput.sources[0] = codeSnippet;
var request_content = JsonConvert.SerializeObject(codeInput);
request.Content = new StringContent(request_content, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var codeOutput = JsonConvert.DeserializeObject<CodeResponse>(responseString);
code.output = codeOutput.Output[0];
}
return View(code);
}
@DemeteorXD
Copy link

what is res?

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