Created
March 24, 2017 16:36
-
-
Save ukcoderj/0c935c90b8df99f7ac08e123a6d3a0d0 to your computer and use it in GitHub Desktop.
Web API with body content and C# / Html Calls
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
// NOTE: This is indicative (using jquery). It's not accurate for the related api. | |
function getValue1() { | |
var request = $.ajax({ | |
url: 'http://localhost:5000/api/values?ID=1', | |
type: "GET", | |
}); | |
request.done(function (returnVal) { | |
var returnAsString = JSON.stringify(returnVal); | |
}); | |
request.fail(function (jqXHR, textStatus) { | |
alert("Request failed: " + textStatus); | |
}); | |
} | |
function valuesPost() { | |
var formData = $('#valuesForm').serializeArray().reduce(function (obj, item) { | |
obj[item.name] = item.value; | |
return obj; | |
}, {}); | |
var request = $.ajax({ | |
url: 'http://localhost:5000/api/values', | |
type: "POST", | |
data: JSON.stringify(formData), | |
contentType: "application/json; charset=utf-8", | |
dataType: "json", | |
}); | |
request.done(function (msg) { | |
alert('Done ->' + msg); | |
}); | |
request.fail(function (jqXHR, textStatus) { | |
alert("Request failed: " + textStatus); | |
}); | |
} |
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
// Calling client | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string answer = ""; | |
//string baseUrl = "http://localhost:56539/"; | |
string baseUrl = "http://169.254.80.80/winauth/"; | |
string getUrl = baseUrl + "api/Values/1"; | |
string postUrl = baseUrl + "api/Values"; | |
try | |
{ | |
// Get Way 1 | |
WebRequest req = WebRequest.Create(getUrl); | |
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; | |
req.Credentials = CredentialCache.DefaultNetworkCredentials; | |
WebResponse resp = req.GetResponse(); | |
StreamReader reader = new StreamReader(resp.GetResponseStream()); | |
answer = reader.ReadToEnd().Trim() + System.Environment.NewLine; | |
// Get Way 2 | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl); | |
request.Credentials = CredentialCache.DefaultNetworkCredentials; | |
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) | |
using (Stream stream = response.GetResponseStream()) | |
using (StreamReader reader1 = new StreamReader(stream)) | |
{ | |
answer += reader1.ReadToEnd() + System.Environment.NewLine; | |
} | |
// POST | |
var httpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl); | |
httpWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials; | |
httpWebRequest.ContentType = "application/json"; | |
httpWebRequest.Method = "POST"; | |
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) | |
{ | |
var param1 = "Hello"; | |
var param2 = "World."; | |
var param3 = "Nice To"; | |
var param4 = "meet you."; | |
/*NOTE: If the commas are not at the end of each property, it can fail silently (they will default to nulls) */ | |
string json = "{\"item1\":\"" + param1 + "\"," + | |
"\"item2\":\"" + param2 + "\"," + | |
"\"item3\":\"" + param3 + "\"," + | |
"\"item4\":\"" + param4 + "\"}"; | |
streamWriter.Write(json); | |
streamWriter.Flush(); | |
streamWriter.Close(); | |
} | |
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); | |
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
{ | |
answer += streamReader.ReadToEnd(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
Console.WriteLine(answer); | |
Console.ReadLine(); | |
} | |
} |
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
// API CONTENT | |
public class ValuesController : ApiController | |
{ | |
public string Get(string id) | |
{ | |
return "Test " + id; | |
} | |
[HttpPost] | |
public string Post([FromBody] FourDetails details) | |
{ | |
return details.Item1 + " " + details.Item2 + " " + details.Item3 + " " + details.Item4 + " "; | |
} | |
} | |
public class FourDetails | |
{ | |
public string Item1 { get; set; } | |
public string Item2 { get; set; } | |
public string Item3 { get; set; } | |
public string Item4 { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment