none
Web Api Error "he remote server returned an error: (500) Internal Server Error." during calling the Post method RRS feed

  • Question

  • Hi Sir,

    I am tring to consume web API using Post method but getting the error   "The remote server returned an error: (500) Internal Server Error." tried many methods and Done many changes. stiil issue is same. I have defined which method i used. so please guide me what mistake  i am doing again and again during the calling web api.

    I wanna let you know one thing that Get method is working very well. i am not facing any issue on that

    I am passing the data in the form of json 

    Data={"searchText":"as","searchKey":null,"txtTemplateName":"GET_FindADP"}

    I am waiting positive response from your side ASAP.

    Below service method i am tring to consume

    [System.Web.Http.Route("Values/Post")]
            [System.Web.Http.HttpPost]
            // POST api/values
            public string  Post([FromBody]string value)
            {

                DataTable _DataTable = new DataTable();
                LookupProvider balLookup = new LookupProvider();
                // _DataTable = balLookup.GET_FindADP(ADPCode, pager);
                string json = "first api"; //JsonConvert.SerializeObject(_DataTable);  testing
               
                return json;
            }

    Method :- 1

    Dim URL As String = "http://localhost/TestingService/Values/Post"


            Dim DATA As String = PagerJsonCona
            Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
            request.Method = "POST"
            request.ContentType = "application/json; charset=utf-8"
            'request.KeepAlive = True
            'request.ContentLength = DATA.Length

            Using webStream As Stream = request.GetRequestStream()

                Using requestWriter As StreamWriter = New StreamWriter(webStream, System.Text.Encoding.ASCII)
                    requestWriter.Write(DATA)
                    requestWriter.Flush()
                    requestWriter.Close()
                End Using
            End Using

            Try
                Dim webResponse As WebResponse = request.GetResponse()

                Using webStream As Stream = If(webResponse.GetResponseStream(), Stream.Null)

                    Using responseReader As StreamReader = New StreamReader(webStream)
                        Dim response As String = responseReader.ReadToEnd()
                        Console.Out.WriteLine(response)
                    End Using
                End Using

            Catch e As Exception
                Console.Out.WriteLine("-----------------")
                Console.Out.WriteLine(e.Message)
            End Try

    Method :- 2

    Dim strurl As String = String.Format("http://localhost/TestingService/Values/Post")
            Dim requestobjPost As WebRequest = WebRequest.Create(strurl)

            requestobjPost.Method = "POST"
            requestobjPost.ContentType = "application/json; charset=utf-8"
            Dim PostDate As String = PagerJsonCona ''"{""title"":""testdate""}"
            Dim streamwriter = New StreamWriter(requestobjPost.GetRequestStream())
            streamwriter.Write(PostDate)
            streamwriter.Flush()
            streamwriter.Close()
            Dim httprespone = CType(requestobjPost.GetResponse(), HttpWebResponse)
            Dim streamReadera = New StreamReader(httprespone.GetResponseStream())
            Dim result = streamReadera.ReadToEnd()

    Method :- 3

     Dim req As HttpWebRequest = CType(WebRequest.Create(strurl), HttpWebRequest)
            req.ProtocolVersion = HttpVersion.Version11
            req.ContentType = "application/json; charset=utf-8"
            req.Accept = "text"
            req.KeepAlive = True
            req.Method = "POST"

            Using stm As Stream = req.GetRequestStream()

                Using stmw As StreamWriter = New StreamWriter(stm)
                    stmw.Write(PagerJsonCona)
                End Using
            End Using

            Using responseReader As StreamReader = New StreamReader(req.GetResponse().GetResponseStream())
                Dim result As String = responseReader.ReadToEnd()
                'ResultXML = XDocument.Parse(result)
                'ResultString = result
            End Using



    Tuesday, June 25, 2019 12:24 PM

All replies

  • attach a debugger on IIS's application pool process to see the exception detail. Then fix the bug on the server side (at least handle the exception and return a more meaningful error message). 

    If you need more help on asp.net API programming ask at https://forums.asp.net/1246.aspx/1?Web+API



    Visual C++ MVP

    Tuesday, June 25, 2019 4:12 PM
  • Thank you Sir ,

    I hit my post request on the fiddler. i got the correct error message information,

    so please suggest me what  should i change in my code to correct it.

    I am waiting positive response form your side.

    ExceptionMessage=No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/octet-stream'.

    ExceptionType=System.Net.Http.UnsupportedMediaTypeException

    Message=The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.

    StackTrace=   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)

       at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)

    Wednesday, June 26, 2019 4:24 AM
  • The request contains an entity body but no Content-Type header

    that is strange, probably you are overwriting the headers somewhere. 

    Move your ContentType assignment to right before making your request. 



    Visual C++ MVP

    Wednesday, June 26, 2019 5:42 AM
  • Hi sir,

    I am passing the content type json even it is showing error  same error . please find the code 

    Private Shared Sub CreateObject(ByVal PagerJsonCona As String)

                Dim URL As String = "http://localhost/TestingService/Values/Post"

     Using httpClient = New HttpClient()
                Dim q1 As RootObject = New RootObject() With {
                        .ID = "001",
                        .Name = "Test1"
                    }
                Dim baseUrl = ""
                ''Dim uri = New Uri(baseUrl & "/api/Values")


                Dim response = httpClient.PostAsync(URL, New StringContent(JsonConvert.SerializeObject(q1), Encoding.UTF8, "application/json"))
                ''Dim response = httpClient.PostAsync(URL, New StringContent(JsonConvert.SerializeObject(PagerJsonCona), Encoding.UTF8,        "application/json"))


                ''Dim response = httpClient.PostAsync(URL, New StringContent(PagerJsonCona, Encoding.UTF8, "application/json"))
                response.Wait()

                    If Not response.Result.IsSuccessStatusCode Then
                    End If
                End Using

     End Sub

    SErvice method code:- 

    public HttpResponseMessage Post(RootObject  value)
            {

                DataTable _DataTable = new DataTable();
                LookupProvider balLookup = new LookupProvider();
                // _DataTable = balLookup.GET_FindADP(ADPCode, pager);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value1");
                return response;

                //return json;
            }

    Wednesday, June 26, 2019 9:31 AM
  • It's not surprising givin you don't have content type set anywhere in this code. Even when setting type doesn't work, avoid set it it surely won't work. 


    Visual C++ MVP

    Wednesday, June 26, 2019 1:44 PM
  • Hi sir,

    Good morning 

    many many thanks to help me.. I have fixed whole bugs as per your suggestion. it is working fine .  :)

    Friday, June 28, 2019 4:31 AM