locked
Post json always null RRS feed

  • Question

  • User-590375999 posted

    Hi i created a post method which should accept json and returns json , but the post method always receives null instead of json

    
        public class OCRController : ApiController
        {
           
    
            [HttpPost]
            [ActionName("zone")]
            public ROCR Post(ROCR r)
            {
             
                return r;
            }
        }
    
        public class ROCR
        {
            public int requestid { get; set; }
            public string content { get; set; }
        }
    
    

    i tested this method using curl tool , the post method always receives null

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"requestid": 1,"content":"Testing.."}'
    http://localhost:62962/api/ocr/zone

    Thursday, October 26, 2017 5:19 AM

All replies

  • User-832373396 posted

    Hi sivapooja,

    Good Day!

    1: At first,please change your method to:

      public class OCRController : ApiController
        {     
           // [HttpPost]
           // [ActionName("zone")]
            public ROCR Post([FromBody]ROCR r)
            {
             
                return r;
            }
        

    2:and url should be:

    http://localhost:62962/api/ocr

    Then it should work;

    3:or please refer to the full working example  .

    1> Add System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register); to Global.asax file

    protected void Application_Start()
            {
               
                AreaRegistration.RegisterAllAreas();
                System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }

    2>Add api controller:

     public class ValuesController : ApiController
        {                 public class myMode {
    
              public int[] parameters { get; set; }
                 public int apiCall { get; set; }
            }
    
            // GET api/<controller>/
            public string Get([FromUri]myMode myMode ,[FromUri]int apiCall ,[FromUri] int[] parameters)
            {
                System.Diagnostics.Debug.WriteLine(myMode.parameters.First() + " and " + myMode.apiCall + " and " +apiCall );// it works
                return "value";
            }

    and call it :

    <script>
                $(document).ready(function () {
                    $.get("api/Values",
                        {
                            parameters: [1, 2],
                            apiCall: 3
                        })
                        .done(function (data)
                        {
                            alert("abc");
                      });

    and resource:https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api#calling-the-web-api-with-javascript-and-jquery

    4: and here is the example to use Postman Tool:

     public class ValuesController : ApiController
        {                 public class myMode {
    
              public int[] parameters { get; set; }
                 public int apiCall { get; set; }
            }
    
            // Post api/<controller>/
            public string Post([FromBody]myMode myMode  )
            {
                            return "value";
            }

    With regards, Angelina Jolie

    Thursday, October 26, 2017 5:40 AM
  • User-590375999 posted

    Hi  Angelina Jolie

    i have tried but does't work

     public class OCRController : ApiController
        {
           
    
            //[HttpPost]
            //[ActionName("zone")]
            public ROCR Post([FromBody]ROCR r)
            {
             
                return r;
            }
        }
    
        public class ROCR
        {
            public int requestid { get; set; }
            public string content { get; set; }
        }

    curl window

    D:\Hanuman\curl\bin>curl -i -H "Content-Type: application/json" -X POST -d '{"requestid":1,"content":"abc"}' http://localhost:62962/api/ocr
    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Server: Microsoft-IIS/10.0
    X-AspNet-Version: 4.0.30319
    X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcc2l2YVxkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDE1XFByb2plY3RzXFpvbmVPQ1JcWm9uZU9DUlxhcGlcb2Ny?=
    X-Powered-By: ASP.NET
    Date: Thu, 26 Oct 2017 08:41:59 GMT
    Content-Length: 4
    
    null
    D:\Hanuman\curl\bin>

    Thursday, October 26, 2017 8:44 AM
  • User-1427819154 posted

    I am having same issues. Help!!!

    Friday, October 27, 2017 1:15 AM
  • User-832373396 posted

    Hi sivappooja,

    After many times tests , here are the finally working examples(Total 5):

    C:\Users\v-tiguo\Downloads\curl-7.53.1\src>curl -i -X POST -H "Accept: application/json"  
    -H "Content-Type:application/json" -d {"parameters":[1,2],"apiCall":3} http://localhost:53015/api/values
    HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RDpcR2l0SHViXEV4YW1wbGVcTVZDR3VvXE1WQ0d1b1xNVkNHdW9cYXBpXHZhbHVlcw==?= X-Powered-By: ASP.NET Date: Fri, 27 Oct 2017 06:13:59 GMT Content-Length: 0 C:\Users\v-tiguo\Downloads\curl-7.53.1\src>curl -i -X POST -H "Accept: application/json"
    -H "Content-Type:application/json" -d {'parameters':[1,2],'apiCall':3} http://localhost:53015/api/values
    HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RDpcR2l0SHViXEV4YW1wbGVcTVZDR3VvXE1WQ0d1b1xNVkNHdW9cYXBpXHZhbHVlcw==?= X-Powered-By: ASP.NET Date: Fri, 27 Oct 2017 06:14:31 GMT Content-Length: 0 C:\Users\v-tiguo\Downloads\curl-7.53.1\src>curl -i -H "Accept: application/json"
    -H "Content-Type:application/json" -d {'parameters':[1,2],'apiCall':3} http://localhost:53015/api/values
    HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RDpcR2l0SHViXEV4YW1wbGVcTVZDR3VvXE1WQ0d1b1xNVkNHdW9cYXBpXHZhbHVlcw==?= X-Powered-By: ASP.NET Date: Fri, 27 Oct 2017 06:14:52 GMT Content-Length: 0 C:\Users\v-tiguo\Downloads\curl-7.53.1\src>curl -H "Accept: application/json" -H "Content-Type:application/json"
    -d {'parameters':[1,2],'apiCall':3} http://localhost:53015/api/values C:\Users\v-tiguo\Downloads\curl-7.53.1\src>curl -i -H "Content-Type:application/json"
    -d {'parameters':[1,2],'apiCall':3} http://localhost:53015/api/values
    HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RDpcR2l0SHViXEV4YW1wbGVcTVZDR3VvXE1WQ0d1b1xNVkNHdW9cYXBpXHZhbHVlcw==?= X-Powered-By: ASP.NET Date: Fri, 27 Oct 2017 06:16:08 GMT Content-Length: 0

    and test controller:

      public class myMode {
    
              public int[] parameters { get; set; }
                 public int apiCall { get; set; }
            }
    
             // POST api/<controller>
            public  OkResult Post( myMode myMode)
            {
                System.Diagnostics.Debug.WriteLine(myMode.parameters.First() + " and " + myMode.apiCall);
                return Ok();
            }

    Output result:

    1 and 3

    So ,please change your code to:

    D:\Hanuman\curl\bin>curl -i -H "Content-Type: application/json" -d {"requestid":1,"content":"abc"} http://localhost:62962/api/ocr
    

    Bests, Angelina Jolie

    Friday, October 27, 2017 6:25 AM