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.

7
  • 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. Commented Sep 7, 2018 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 Commented Sep 8, 2018 at 0:15
  • possible duplicate stackoverflow.com/questions/6566456/… Commented Sep 8, 2018 at 0: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. Commented Sep 10, 2018 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. Commented Sep 10, 2018 at 17:15

2 Answers 2

6

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"].

Sign up to request clarification or add additional context in comments.

Comments

3

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.