How do I make an api call in C#? The javascript code works as jsfiddle but not in my console app
I have this code that works in javascript but does not work in the c# code I'm getting a 500
var jsoninput = { inputParams:
JSON.stringify({
SecurityCode: 'xxx123',
StatusID: 'O',
FromShiftDate: '01/16/2020',
ToShiftDate: '01/22/2020',
ExcludePerDiem: false,
ExcludeContracts: false,
ExcludePermPlacement: false
}, null, 2)
};
var jqXHR = $.ajax({
type: 'POST',
url: 'https://example.com/api.asmx/Agency_ListOrdersByShiftDate',
data: JSON.stringify(jsoninput),
cache: false,
async: true,
crossDomain: true,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
var odata = JSON.parse(data.d);
if (odata.success) {
alert(data.d);
//Do something
//Reference the orders directly: odata.data.Orders
//or access raw: data.d
} else {
alert(data.d);
//do something with errors
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Status : " + textStatus + " Message : "+ errorThrown);
//Error Handling
}
});
trying to get this to work on a windows console c# net application but getting a 500 code
string apiUrl = "https://example.com/api.asmx/Agency_ListOrdersByShiftDate";
var input = new
{
SecurityCode = "xxx123",
StatusID = "O",
FromShiftDate = "01/20/2020",
ToShiftDate = "01/30/2020",
ExcludePerDiem = false,
ExcludeContracts = false,
ExcludePermPlacement = false
};
string inputJson = (new JavaScriptSerializer()).Serialize(input);
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
string json = client.UploadString(apiUrl, inputJson);
I have now have tried
var httpClient = new HttpClient();
var result = await httpClient.PostAsync(apiUrl, input.AsJson());
with the results:
StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { jsonerror: true Cache-Control: private Date: Thu, 23 Jan 2020 17:23:06 GMT Server: Microsoft-IIS/10.0 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Content-Length: 91 Content-Type: application/json; charset=utf-8 }
inputJson
and check if your json is valid – LinkedListT