0

I am trying to POST Json data to API and read the value from the key using the HttpContext.Current.Request object.

Client Side:

var data = { Name: "Tom" };

$.ajax({
    type: "POST",
    url: url,
    data: JSON.stringify(data),
    dataType: "json",
    contentType: "application/json",
    success: function (response) {

    }
    });

In API:

[HttpPost]
[Route("UploadProduct")]
public HttpResponseMessage Upload()
{

if (string.IsNullOrEmpty(HttpContext.Current.Request["Name"]))
                return "Name is missing";

//... removed for brevity

}

Why key Name is always empty ?

I know Json data will bind to model object only. But like to know if it's possible to get data from HttpContext.Current.Request object by making some changes in client side ?

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • Why would you want to get the data from `HttpContext.Current.Request` when you can bind it to a model as parameter? – Yom T. Jan 14 '19 at 17:51
  • 1
    Does your `data` (as in `JSON.stringify(data)`) contain a 'Name' property? You've not included it either way in the question. – freedomn-m Jan 14 '19 at 17:53
  • @jom The API is already use with client , so dont want to break things. Also is there any way to read data apart from `HttpContext.Current.Request` ? – Shaiju T Jan 14 '19 at 17:54
  • Can you post a sample of this data you are passing to the method? – Yom T. Jan 14 '19 at 17:55
  • @stom Also, why do you think model binding the data in the method breaks things? – Yom T. Jan 14 '19 at 17:59
  • Have you tried `HttpContext.Current.Request.Form.AllKeys` to check what's actually being passed? Have you checked the browser network tab to see what's being sent? – freedomn-m Jan 14 '19 at 18:00
  • @jom added the sample data. – Shaiju T Jan 14 '19 at 18:02
  • Do you need the `stringify` for any specific reason? Try without this (and remove contentType): `$.ajax({ type: "POST", url: url, data: data, dataType: "json", success: ...` With stringify you're only posting a single json value (it's not auto-expanded by web-api), not a form, so `Request["form-field-name"]` doesn't have a form to get the field name from. This is handled when you use model-binding. – freedomn-m Jan 14 '19 at 18:12

1 Answers1

2

OK, try this:

Change contentType to application/x-www-form-urlencoded, remove the JSON.stringify() and just pass the JSON data as is. With this you should be able to get the form values with this.Request.Form["Name"] or simply this.Request["Name"] or even HttpContext.Current.Request["Name"].

When you POST data to server (particularly with content types other than application/x-www-form-urlencoded), the content is being placed in the Request body, and so it's not going to be available for reading in the Request.Form name-value collection object.

For nested data, you can query the values just like you would with Javascript object literal, something like:

var data = {
  Name: "Tom",
  Address: {
    Street: "ABC"
  }
}
this.Request.Form["Address[Street]"] // ABC

Although it's always better to use model binding whenever possible.

Yom T.
  • 8,760
  • 2
  • 32
  • 49
  • Hi, can you review your sentence `so it's not going to be available for reading in the Request.Form`, I think it should be `so it's going to be available for reading in the Request.Form`. – Shaiju T Jan 23 '19 at 13:57
  • 1
    @stom When you post data as JSON for example, it should not go in `Request.Form`. If you need to post them as JSON, you could use model binding. See [this post](https://stackoverflow.com/a/21579294/3634538) for example. – Yom T. Jan 23 '19 at 15:51