Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:54:46+00:00 2026-06-13T12:54:46+00:00

We have an MVC (MVC4) application which at times might get a JSON events

  • 0

We have an MVC (MVC4) application which at times might get a JSON events POSTed from a 3rd party to our specific URL (“http://server.com/events/“). The JSON event is in the body of the HTTP POST and the body is strictly JSON (Content-Type: application/json – not a form-post with JSON in some string field).

How can I receive the JSON body inside the controller’s body? I tried the following but didn’t get anything

[Edit]: When I say didn’t get anything I meant that jsonBody is always null regardless of whether I define it as Object or string.

[HttpPost]
// this maps to http://server.com/events/
// why is jsonBody always null ?!
public ActionResult Index(int? id, string jsonBody)
{
    // Do stuff here
}

Note that I know if I give declare the method with a strongly typed input parameter, MVC does the whole parsing and filtering i.e.

// this tested to work, jsonBody has valid json data 
// that I can deserialize using JSON.net
public ActionResult Index(int? id, ClassType847 jsonBody) { ... }

However, the JSON we get is very varied, so we don’t want to define (and maintain) hundreds of different classes for each JSON variant.

I’m testing this by the following curl command (with one variant of the JSON here)

curl -i -H "Host: localhost" -H "Content-Type: application/json" -X POST http://localhost/events/ -d '{ "created": 1326853478, "data": { "object": { "num_of_errors": 123, "fail_count": 3 }}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:54:47+00:00Added an answer on June 13, 2026 at 12:54 pm

    It seems that if

    • Content-Type: application/json and
    • if POST body isn’t tightly bound to controller’s input object class

    Then MVC doesn’t really bind the POST body to any particular class. Nor can you just fetch the POST body as a param of the ActionResult (suggested in another answer). Fair enough. You need to fetch it from the request stream yourself and process it.

    [HttpPost]
    public ActionResult Index(int? id)
    {
        Stream req = Request.InputStream;
        req.Seek(0, System.IO.SeekOrigin.Begin);
        string json = new StreamReader(req).ReadToEnd();
    
        InputClass input = null;
        try
        {
            // assuming JSON.net/Newtonsoft library from http://json.codeplex.com/
            input = JsonConvert.DeserializeObject<InputClass>(json)
        }
    
        catch (Exception ex)
        {
            // Try and handle malformed POST body
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
    
        //do stuff
    
    }
    

    Update:

    for Asp.Net Core, you have to add [FromBody] attrib beside your param name in your controller action for complex JSON data types:

    [HttpPost]
    public ActionResult JsonAction([FromBody]Customer c)
    

    Also, if you want to access the request body as string to parse it yourself, you shall use Request.Body instead of Request.InputStream:

    Stream req = Request.Body;
    req.Seek(0, System.IO.SeekOrigin.Begin);
    string json = new StreamReader(req).ReadToEnd();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: We have MVC 4.0 web application with display myOrders which returns json result
I've an ASP.NET MVC application which I've upgraded from 2.0 to 3.0 in the
The title is very clear. I have an application-BaseController which inherits from Controller-class now.
I have an ASP.NET MVC 4 application which uses the new ASP.NET Web API.
I have asp.net mvc 4 web application project on my server in IIS Directory.
I have a simple MVC 4 (Beta) Application and just observed something that I
I have a problem with my MVC 4 Application. I have a Model with
I have a layered web application that I built with ASP.NET MVC 4, WebAPI
I have a ASP.NET MVC 4 application with Web API. It's working great. But
I have a TorrentLeech application with ASP.Net MVC 4 and MonoTorrent library. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.