Click here to Skip to main content
14,843,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am sending the JSON data to client as below Code

they are created a Web API as per mentioned methos and they tested the in 'postman' with sample JSON Data and its saved their DB successfully,
        public HttpResponseMessage InsertPatDet([FromBody]List<Patient> pt)

but they said that with my code they are getting no recors(0)

What I have tried:

<pre>//Create a request using a URL that can receive a post.         

            string url = "https://url";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = GetJSonData();// "{\"name\":\"Here is the json text 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentType = "application/json; charset=utf-8 ";
            request.Accept = "application/json";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.

            //List<byte> ListByte = new List<Byte>(byteArray);

            dataStream.Write(byteArray, 0, byteArray.Length);

            // Close the Stream object.
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            // Clean up the streams.
Updated 22-Sep-20 4:40am
F-ES Sitecore 22-Sep-20 4:56am
Is the url right? Is the json correct? Does your API method get called? Are there any exceptions raised?
Parazival 22-Sep-20 5:58am
Url is correct and Json also Correct
API method is hitting but data is not going

string postData="[{\"FirstName\":\"xyz kumar\",\"DateOfBirth\":\"1996-06-07\",\"LastName\":\"\",\"Gender\":\"M\",\"PatientId\":\"456HJKGHJ\",\"Mobile\":\"9999999999\",\"KIN\":[{\"Name\":\"gvhgv kjbj,\",\"Visits\":"
+"[{\"date\":\"2020-06-24T00:00:00\"},{\"date\":\"2020-06-26T00:00:00\"},{\"date\":\"2020-06-28T00:00:00\"}]}]},{\"FirstName\":\"abc kumari\",\"DateOfBirth\":\"1996-06-07\",\"LastName\":\"\","
+"{\"date\":\"2020-07-26T00:00:00\"}]}]} ]";

1 solution

request.ContentType = "application/x-www-form-urlencoded";
If you're sending JSON, then you need to use the correct MIME type.
request.ContentType = "application/json";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900