0
votes

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 }

1
Have you tried passing the exact same input parameters to the working version? - PaulF
It seems they are different Endpoints. Try with the exact same, first. - Fildor
Also: " Important We don't recommend that you use the WebClient class for new development. Instead, use the System.Net.Http.HttpClient class." WebClient Class - Fildor
Please edit your question to improve it or add information. Comments are not a good place for that. - Fildor
debug inputJson and check if your json is valid - LinkedListT

1 Answers

0
votes

had to create an object of LastByShiftParams serialize that into a property of inputParams in another object LinkInParams and then serialize that object

var jsoninput = new LinkInParams(new LastByShiftParams(apiKey, "01/21/2020", "01/29/2020"));


         public LinkInParams(object inObject)
                {
                    inputParams = JsonConvert.SerializeObject(inObject, Formatting.Indented);
                }  

      class LastByShiftParams
        {  
     public string SecurityCode ;
            public string StatusID ;
            public string FromShiftDate ;
            public string ToshiftDate ;
            public bool ExcludePerDiem ;
            public bool ExcludeContracts ;
            public bool ExcludePermPlacement;
        }