5

Context: My application is behind a central login app, whenever the user apply access to my application, my application got a http request contain the user info. And I need to retrieve the user info from the HttpRequest Body.

This is what I tried so far:

currentContext.HttpContext.Request.Query["user-name"].toString();  // got nothing

using (var reader = new StreamReader(currentContext.HttpContext.Request.Body))
{
    var body = reader.ReadToEnd();
}   // I can get the raw HttpRequest Body as "user-name=some&user-email=something"

Is there any method I can use to parse the parameters and values from the Request.Body? I tried the following, got nothing either.

HttpContext.Item['user-name'] \\return nothing Request.Form["user-name"] \\ return nothing

and the reason I am not be able to use model binding is, in the HttpRequest body, the key name is "user-name", and in c#, I can't create a variable with a "-"

Meanwhile, in the my .net 4.6 application, Request["KeyName"].toString() works just fine.

ekad
  • 14,436
  • 26
  • 44
  • 46
Kun Li
  • 91
  • 1
  • 1
  • 8
  • 2
    You can use `Request.Form["key"]`. If it still doesn't work, please show us how you post the data and the entire action method. – Win Sep 07 '18 at 23:37
  • last i checked there was no way to convert this into an object. This was because the usual way is to pass json to the body – Neville Nazerane Sep 08 '18 at 00:15
  • possible duplicate https://stackoverflow.com/questions/6566456/how-to-serialize-an-object-into-a-list-of-url-query-parameters – Neville Nazerane Sep 08 '18 at 00:15
  • @NevilleNazerane I am not trying to bind the parameters to an object. Just trying to the get the values. And the I have no control over the central login app. I believe the central login app is sending the parameters to my application using XML. – Kun Li Sep 10 '18 at 16:19
  • @Win I tried Requst.Form["Keyname"], got null. And I don't have access to the source of the central login app. So I don't know how they are posting the data. – Kun Li Sep 10 '18 at 17:15
  • Why don't you use the normal model binding features? What about those don't work for you? – mason Sep 10 '18 at 18:18
  • the deserializing part of the question i sent is the same. you can use the same method to deserialize – Neville Nazerane Sep 10 '18 at 23:12

2 Answers2

4

I figured out a way to convert the raw HttpRequest Body to a Query String, then read parameters from it.

Here is the code:

var queryString = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(requestBody);

string paramterValueIWant = queryString["KeyName"];

There is one problem though, when the KeyName doesn't exist in the body, it will throw an exception. So you have to null check or do a try catch.

Still I feel like there should be a better way to read the parameter, as I mentioned, in my .net 4.6 application, all I need to do is Request["KeyName"].

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
Kun Li
  • 91
  • 1
  • 1
  • 8
2

Assuming that we are talking about POST/PUT/PATCH call, you can use
Request.Form["KeyName"] in your API method and set the 'contentType' of the Ajax request as application/x-www-form-urlencoded
Notice that Request is automagically available inside your method. No need to explicit call it.

When using GET/DELETE call i prefer to use

[HttpGet("{UserId}")] // api/User/{UserId}
public IActionResult Get(int UserId){
  // do stuff calling directly UserId
}

Or with PUT/PATCH

[Route("User/{EntityId}/{StatusFilter}")] // api/User/{EntityId}/{StatusFilter}
[HttpPut] 
public IActionResult Put(int EntityId, int StatusFilter){
  // do stuff calling directly EntityId and StatusFilter
}

where you can then still take data from the Body using Request.Form["KeyName"]