How to receive json in ASP.NET web api?

35,725

Your Web API model needs to be same as javascript request object

For example,

public class SomeRequest
    {

        public string data1{ get; set; }
        public string data2{ get; set; }
        public string data13{ get; set; }

    }

In Javascript , your request object and javascript call should be like this

 var requestObj = {
                "data1": data1,
                "data2": data2,
                "data3": data3,

            };

And post url and requestObj in javascript

Access in Web API like this

  public void PushSensorData(SomeRequest objectRequest)
        {
objectRequest.data1
objectRequest.data2
objectRequest.data3
Share:
35,725
null
Author by

null

#SOreadytohelp

Updated on August 21, 2022

Comments

  • null
    null over 1 year

    I have an external company pushing data to one of our server, they are going to send JSON data. I need to create a POST api to receive it. This is what I have so far

    [System.Web.Http.HttpPost]
    [System.Web.Http.ActionName("sensor")]
    public void PushSensorData(String json)
    {
        string lines = null;
        try
        {
              System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
              file.WriteLine(json);
              file.Close();
              //JSONUtilities.deserialize(json);
        }
        catch (Exception ex)
        {
            MobileUtilities.DoLog(ex.StackTrace);
        }
    }
    

    I am testing it by send json data using fiddler but json is null. This is the raw data from fiddler.

    POST http://localhost:31329/mobileapi/pushsensordata/ HTTP/1.1
    User-Agent: Fiddler
    Host: localhost:31329
    Content-Type: application/json
    Content-Length: 533
    
    {
    "id": {
        "server": "0.test-server.mobi",
        "application": "test-server.mobi",
        "message": "00007-000e-4a00b-82000-0000000",
        "asset": "asset-0000",
        "device": "device-0000"
    },
    "target": {
        "application": "com.mobi"
    },
    "type": "STATUS",
    "timestamp": {
        "asset": 0000000
        "device": 00000000,
        "gateway": 000000,
        "axon_received_at": 00000,
        "wits_processed_at": 000000
    },
    "data_format": "asset",
    "data": "asset unavailable"
    }
    
  • null
    null over 8 years
    I am trying to use fiddler to test the program since I have no clue what the other company is using. I am passing json in the request body.
  • hbulens
    hbulens over 8 years
    Have you tried something else than JSON, like a simple string? If that works, try passing your JSON as a simple string rather than a JSON object. You accept a string in your web api, it's quite normal you don't get results here. As a test, you could change your data type from string to dynamic, see what happens then.
  • Admin
    Admin over 8 years
    JSON.stringify is irrelevant as the JSON @hbulens is receiving is already well-formed JSON.
  • hbulens
    hbulens over 8 years
    I don't think you read my answer all the way through. ASP.NET Web API is particulary sensitive when it comes to correctly formatted parameters. I've had this problem multiple times and stringifying json objects works pretty much every time