Showing posts with label Webapi. Show all posts
Showing posts with label Webapi. Show all posts

Friday, May 13, 2016

Common Mistake :Asp.net MVC or Web Api and POST Json request

I'm sure most of the folks must be well aware of Post request that is send as Json Raw body request.
I come across very common and trivial mistake that I did when creating my web api request. I was working with my scripting guy and he asked me to form Json request in some format and end up spending an hour to figure out the problem related to my json which was not coming fine.
#Problem: Always check the Variable instead the main class or Model name. Otherwise you will end with JSON request coming in .net web.api as null values.
   
public class SOME_VIEW_MODEL
    {
        public DateTime? DOB { get; set; }
        public IList  childViewModel{ get; set; }
    }

public virtual JsonResult GetMeSomeResults(SOME_VIEW_MODEL someViewModel)
{

}
With respect to above data model design the json can be formed with two request body. #1 Request Body
   {
      "DOB": "",
      "childViewModel": [
        {
          "DOB": "1992-03-21"
        },
        {
          "DOB": "1992-03-21"
        }
      ]
    }
{
    "someViewModel":
    {
      "DOB": "",
      "childViewModel": [
        {
          "DOB": "1992-03-21"
        },
        {
          "DOB": "1992-03-21"
        }
      ]
    }
}
Mind it and double check always that you are using variable in request instead a class name. It is basic concept however we tend to make such mistake. In this case it has to be "someViewModel" instead SOME_VIEW_MODEL

Saturday, January 24, 2015

Web API :Utility To read Json output From WebResponse


Introduction

The below utility fetches the output from API request through HTTP in JSON format.


public static class JsonRequestStreamUtility
    {
        public static WebRequest CreateRequest(string url, string method = WebRequestMethods.Http.Get, string body = null)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = method;
            webRequest.Accept = "application/json";
            webRequest.Headers.Add("Accept-Charset", "utf-8");
            webRequest.ContentType = "application/json";

            webRequest.Proxy = new System.Net.WebProxy()
                {
                    Address = new Uri("xyz"),
                    UseDefaultCredentials = true,
                };
            
            if (body == null)
                return webRequest;

            using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                streamWriter.Write(body);
            }
            return webRequest;
        }

        public static string ExecuteJsonResponseStream(WebRequest webRequest)
        {
            
            if (webRequest == null) throw new ArgumentNullException("webRequest");

              try
            {

                // Get the stream associated with the response.
                Stream receiveStream = webRequest.GetResponse().GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format. 
                StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                return  readStream.ReadToEnd();
                
            }
            catch (WebException we)
            {
                              
                return string.empty;
            }
        }
    }