none
StatusCode: 500, ReasonPhrase: 'Internal Server Error RRS feed

  • Question

  • Hi,

    System is throwing Internal server error 500 when HTTP post request is called from client

                
    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using Newtonsoft.Json;

    namespace TestClient
    {
        public class ClinetPOHeader
        {
            public int OrderNo { get; set; }
            public string Vendor { get; set; }
        }
        public class ClientPOLine
        {
            public int OrderNo { get; set; }
            public int OrderLineNo { get; set; }
            public string Product { get; set; }
        }
        public class ClientCompany
        {
            public string Company { get; set; }
        }

        public class RootObject
        {
            public List<ClinetPOHeader> header { get; set; }
            public List<ClientPOLine> line { get; set; }
            public ClientCompany company { get; set; }
        }
        class Program
        {

    static HttpClient client = new HttpClient();

            static void Main(string[] args)
            {
                client.BaseAddress = new Uri("http://localhost:8081/");
                RootObject objectToSerialize = new RootObject();
                objectToSerialize.header = new List<ClinetPOHeader> 
                              {
                                 new ClinetPOHeader { OrderNo = 1000, Vendor = "xxxx" },
                                 new ClinetPOHeader { OrderNo = 1001, Vendor = "xxxx" }
                              };
                objectToSerialize.line = new List<ClientPOLine>
                                {
                                    new ClientPOLine{OrderNo = 1000,OrderLineNo=1,Product="PRD1"},
                                    new ClientPOLine{OrderNo = 1000,OrderLineNo=2,Product="PRD2"},
                                    new ClientPOLine{OrderNo = 1001,OrderLineNo=1,Product="PRD1"},
                                };
                objectToSerialize.company = new ClientCompany{Company = "USSI"};
                SendOrders(objectToSerialize);
                Console.WriteLine("Press Enter to quit.");
                Console.ReadLine();
            }

            static void SendOrders(RootObject objectToSerialize)
            {
                // Create a request
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
                                                                    "api/Home/Orders/");
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Content = new StringContent(JsonConvert.SerializeObject(objectToSerialize), System.Text.Encoding.UTF8, "application/json");

                var result = client.SendAsync(request).Result;
                // Display result to console
                if (result.IsSuccessStatusCode)
                {
                    Console.WriteLine("Orders are sent.");
                }
                else
                {
                    Console.WriteLine(result.StatusCode);
                }
            }
        }
    }


    Saturday, July 15, 2017 10:14 AM

All replies

  • What's the Api? Can u show us the codes?

    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report



    Saturday, July 15, 2017 10:16 AM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace MVC.Controllers
    {
        public class POLine
        {
            public int OrderNo { get; set; }
            public int OrderLineNo { get; set; }
            public string Product { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace MVC.Controllers
    {
        public class POHeader
        {
            public int OrderNo { get; set; }
            public string Vendor { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    namespace MVC.Controllers
    {
        public class HomeController : ApiController
        {
    List<POHeader> poHeaders = new List<POHeader>();
            [HttpPost]
            public List<POHeader> Orders(List<POHeader> poHeaderList, List<POLine> poLineList )
            {
                POHeader orderHeader;
                foreach(var header in poHeaderList)
                {
                    orderHeader = new POHeader();
                    foreach(var line in poLineList)
                    {
                        orderHeader.OrderNo = header.OrderNo;
                        orderHeader.Vendor = header.Vendor;
                    }
                    poHeaders.Add(orderHeader);
                }
                return poHeaders;
            }
        }
    }

    Saturday, July 15, 2017 11:30 AM
  • System throws error  in the below line(Client)

    var result = client.SendAsync(request).Result;

    Saturday, July 15, 2017 11:32 AM
  • System throws error  in the below line(Client)

    var result = client.SendAsync(request).Result;

    This is not the MVC forum. This is not a Web forum. Where you should be posting to is the ASP.NET forum.

    http://forums.asp.net/

    Saturday, July 15, 2017 11:52 AM
  • System throws error  in the below line(Client)

    var result = client.SendAsync(request).Result;

    Well,Please debug at your webApi to see where the exception is thrown out?

    ASP.NET Forum
    StackOverFlow
    FreeRice Donate
    Issues to report



    Saturday, July 15, 2017 12:11 PM
  • 500 errors generally indicate something went wrong on the server. In ASP.NET implementations it generally means an exception occurred. Since you're calling your own API you'll need to debug that code.

    Please post questions around your API in the ASP.NET forums.

    Saturday, July 15, 2017 7:47 PM
  • Hello Rahdirs,

    I test you code and I notice the parameter of Orders method doesn't match the request content . You could fix it as below.

    [HttpPost]
    public List<POHeader> Orders([FromBody]RootObject ro)
            {
                return null;
            }

    The result:

    Best regards,

    feih-7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by ThankfulHeart Monday, July 17, 2017 9:13 AM
    Monday, July 17, 2017 6:48 AM