Reading body in PostRequest asp.net core - c#

I've been trying to read the body of a post request in a controller for a while but I keep getting the error that synchronous actions are not allowed. This is also meant to be used as an API so I can't just read the values trough a textbox like normal.
This is my code right now but I have no idea if this is right because this is based on Java code and my experience with asp.net core is limited.
public IActionResult Test()
{
string body = "";
StreamReader reader = new StreamReader(Request.Body);
while (reader.ReadLine() != null)
{
body += reader.ReadLine();
}
return Ok(body);
}
I'm just trying to read the body so I have an idea on how I can filter on the header of the json input.
Is there maybe a different way that I can do this? Or am I forgetting something important?

Thanks to the advice of Chetan Ranpariya, I found the answer. Like Chetan said I needed to use a model class which is pretty easy to do. I just added a folder named Models and added a class name User with this code:
public class User
{
public string UserName { get; set; }
public string Password { get; set; }
}
And then my previous method turned into this:
public IActionResult Test(User user)
{
return Ok(new { username = user.UserName, password = user.Password });
}
The only thing that I needed to pay attention to was the json body of my request. The header needs to correspond with the property name so it looked like this:
{
"UserName": "Admin",
"Password": "AdminPass"
}
And now I can continue working on my project.

Related

Web API POST data is null.... sometimes

I have a WebApi I built that sends emails. I also have a console application that runs everynight, generates a basic report, and emails it to me, through the API.
It was working perfect, until randomly one day I stopped getting the emails. (I say randomly, but I'm sure there was something that happened, - that's why I'm here.) If I send a short HtmlMessage, like <h1>Hi!</h1> it works, but the longer email it actually generates hits the server as null. I'm not sure if I made a change or something that broke this, but I definitely didn't change anything in the email's html.
I have a Mailer class:
public class Mailer
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string HtmlMessage { get; set; }
}
Here is my WebAPI:
[HttpPost]
[Route("api/sendmail")]
public void Sendmail(Mailer mailer) //public void Sendmail([FromBody] Mailer mailer) tried with and without [FromBody] and neither work
{
/* A bunch of code that doesn't matter */
}
And here is the code that calls the API:
static void Main() {
string message;
/* a bunch of stuff that generates the message */
SendEmail(message);
}
static void SendEmail(string message) {
var data = new Mailer { From = "foo#foo.com", To = "timothy#foo.com", Subject = "Daily Report", HtmlMessage = message };
var data2 = new Mailer { From = "foo#foo.com", To = "timothy#foo.com", Subject = "Daily Report", HtmlMessage = "<h1 style=\"color: red;\">HI</h1>" };
// I was using new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data); but changed to JSON.net as an attempt to fix
var json = Newtonsoft.Json.JsonConvert.SerializeObject(data); // THIS DOES NOT WORK?! mailer in Sendmail is null.
//var json = Newtonsoft.Json.JsonConvert.SerializeObject(data2); // THIS WORKS?!
var url = "https://server.com/api/sendmail";
using (var client = new WebClient())
{
client.Headers.Add(_headers);
client.Headers.Add("Content-Type", "application/json");
client.UploadString(url, json);
}
}
Any help is appreciated! Thank you in advance!
Well, I feel dumb, but I was able to figure it out. I have a disclaimer at the bottom of the email, where I originally had (C) but replaced it with a copyright sign (©). That appears to have broken it. I replaced it with © and it works perfect now.
So, the issue was this character, that I assume WebAPI declined or was unable to deserialize into the Mailer class.
Anyways, it's fixed! Hopefully this helps someone else out down the road!

complex object is empty when use in Web API input

I have a method in Web API that I used the object as input, but when I try to run the API using URI the fields inside the object are Null.
this is my method:
[HttpGet]
[Route("AddUser/{user}")]
public async Task<string> CreateUser([FromUri]AddUser user)
{
//LoansApiTrace.Trace.Verbose(EventId.Start, () => string.Format("{0}: {1}", "AddUser", user.));
string Exception = await Repository.AddUserAsync(user);
return Exception;
}
This is AddUser object:
public class AddUser
{
public string UserEmailAddress { get; set; }
public string PasswordHash { get; set; }
public string Salt { get; set; }
public string RemoteRefNumber { get; set; }
}
and this is the URI:
http://localhost:59509/Adduser/user=test#yahoo.com,pass,salt,remref/
it goes to the method but UserEmailAddress , PasswordHash ,..all 4 are empty.
This is a really a bad practice to pass secret data through URI like you're doing. Then I will not attempt to give a solution for that to work.
The best practice is to pass that kind of data through your request body and use Http POST method :
[HttpPost]
[Route("AddUser/{userId}")]
public async Task<string> CreateUser(string userId, [FromBody]AddUser user)
{
// Find a user by userId
// Then update the user data.
}
you use an URI like this => http://localhost:59509/Adduser/12345 where 12345 is the user id.
you need to make sure that the selected HTTP method is POST
you need to write the data of AddUser into the request body
It also recommanded to use HTTPS when user need to send that type of data.
Consider Using POST If Applicable
While it may not be do-able, you may want to consider adding these fields within a <form> and simply posting them to the server in the body as opposed to using the actual URL itself. Passwords, salts and hashes generally aren't something that you want to get passed around like that.
If You Must Use A GET
Have you tried passing the values in as proper query-string parameters instead?
http://localhost:59509/Adduser/user?UserEmailAddress=test#yahoo.com&PasswordHash=abc&Salt=123&RemoteRefNumber=foo
This should set the following properties based on your current routes:
user = "user"
UserEmailAddress = "test#yahoo.com"
PasswordHash = "abc"
Salt = "123"
RemoteRefNumber = "foo"
MVC has to have some idea of how to bind these properties to those on your class, so unless the names match as expected, it will not know how to map something like "user" to "UserEmailAddress". As mentioned earlier, this isn't ideal and can present all sorts of security issues (so only use something like this on prototype / non-production environments).

Displaying JSON data acquired from a WebRequest in ASP.NET 5 using MVC 6

this is my first question, so I apologize if I mess up the formatting or do this wrong in general, feel free to give me pointers, I'm always open to learn.
Anyway, my issue at hand is that I have a web application I'm working on using ASP.NET 5 and MVC 6, all up to date, and so far for testing, I've been using the localdb and working with fake data. Now, I have a url, with an API token, and login info, and I am using a WebRequest to get the data and stream it with a StreamReader into a variable, writing it, and then trying to return it.
WebRequest req = WebRequest.Create(#"https://url.fortheapi.com/api/search/things?criteria=" + userInput);
req.Method = "GET";
req.Headers["username"] = "user";
req.Headers["password"] = "password";
req.Headers["token"] = "token";
StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream());
var responseData = responseReader.ReadToEnd();
Response.WriteAsync(responseData);
return View(responseData);
Here is where I'm stuck because I am not sure exactly how to pass it to the view as model data, I have no model currently, and I want to make one based on this database and use Entity Framework to work with it like I have been with the localdb. If there's a better way to do it, please feel free to present it. I will accept all the help I can get right now.
You need to create POCO classes to represent the data you receive from your api call. Once you get the response data, you may simply use a javascript serialize to deserialize the response to an object of your POCO class. You can pass this to your view.
public async Task<ActionResult> Contact()
{
var req = WebRequest.Create(#"yourApiEndpointUrlHere");
var r = await req.GetResponseAsync().ConfigureAwait(false);
var responseReader = new StreamReader(r.GetResponseStream());
var responseData = await responseReader.ReadToEndAsync();
var d = Newtonsoft.Json.JsonConvert.DeserializeObject<MyData>(responseData);
return View(d);
}
Assuming your api returns json data like this
{ "Code": "Test", "Name": "TestName" }
and you have created a POCO class called MyData which can be used to represent the data coming back from the api. You may use json2csharp to generate your C# classes from the json response you received from your api.
public class MyData
{
public string Code { get; set; }
public string Name { set;get;}
//Add other properties as needed
}
Now your view should be strongly typed to this POCO class
#model MyData
<h2>#Model.Code</h2>
<h2>#Model.Name</h2>
If what you are receiving is JSON, you can accomplish this is many ways.
One would be to wrap the code you've posted into a JSON Result typed Action. A very simplistic example below:
[HttpPost]
public JsonResult GetIncidentId(int customerId, string incidentNumber)
{
JsonResult jsonResult = null;
Incident incident = null;
try
{
incident = dal.GetIncident(customerId, incidentNumber);
if (incident != null)
jsonResult = Json(new { id = incident.Id });
else
jsonResult = Json(new { id = -1 });
}
catch (Exception exception)
{
exception.Log();
}
return jsonResult;
}
Calling it via Javascript from the view and manually populating your form (meh).
Or more elegantly, you could create an MVC model to hold the data you receive and serialise the JSON into that model. An example of which is below:
From: http://www.newtonsoft.com/json/help/html/deserializeobject.htm
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
string json = #"{
'Email': 'james#example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
]
}";
Account account = JsonConvert.DeserializeObject<Account>(json);
Hope this helps and good luck with your app!

Building json response

I'm working on a self hosting rest api used to monitor de status of several servers.
I was tasked that, when everything is working correctly, I should only return
{"response":"ok"}
But, when there's an error on queried server, or servers, I must return
{ "response" : [ {"agent":"<server>:<port>","port":"<port>" ,"Error":"<Description of the error>"} ] }
I was thinking on building a helper class to build object on this schema and returning them over the rest api
public class HelperErrorResponseClass
{
public string agent { get; set; }
public string port { get; set; }
public string Error { get; set; }
}
This is no problem, the issue is, how to deal when everything it ok. I have this Api response helper class
public class Response
{
public string response { get; set; }
}
But I'm seeing that I'll need to change the response property to List<HelperErrorResponseClass> in order to send the error response. Do you think that, if I stringify the List<HelperErrorResponseClass> object with Json.Net it will be returned in the desired format?
Edit: Forgot to add that, I-m using Web Api to build the rest service.
UDPATE:
After further research, I found a way to work this out.
Following this post, I was able to rewrite the helper classes like this
[DataContract]
[KnownType(typeof(List<HelperErrorResponseClass>))]
public class Response
{
[DataMember]
public object response { get; set; }
}
[DataContract]
public class HelperErrorResponseClass
{
[DataMember(EmitDefaultValue = false)]
public string agent { get; set; }
[DataMember(EmitDefaultValue = false)]
public string port { get; set; }
[DataMember(EmitDefaultValue = false)]
public string error { get; set; }
}
This work to fulfill my and my client needs... except for one little thing. When I get the result from a List, and given that I added the KnownTypes directive, my response is now this
{"response":[{"__type":"HelperErrorResponseClass:#AppCommonLib","Error":"ERROR","InstanceId":"<InstanceId> : <Port>","PortType":"<PortType>"},{"__type":"HelperErrorResponseClass:#AppCommonLib","Error":"ERROR","InstanceId":"<InstanceId> : <Port>","PortType":"<PortType>"}]}
Any idea how to get rid of that __type property of the response? make that it must be explicit to only return the declared properties of the helper class?
Simplest way to deal with this is to set the return type on the handling function to string, then you can check for errors and do something like;
//pseudo code to give an idea
if (errorsList.Count() > 0)
{
return JsonConvert.SerializeObject(errorsList);
}
else
{
return JsonConvert.SerializeObject(new Response("ok"));
}
Now this being said... Unless the people providing requirements aren't at all flexible you should just redo the design. How about just returning the errors array and the person calling the API can infer that if it's length is 0 then everything is working OK. Seems pretty straight forward, right? You could also just put all the properties on one object and those fields would just come back as null or empty strings. Or you could change you serializer settings to exclude them if they don't have a value.
Keep things simple and use an anonymous type.
if (condition)
{
return JsonConvert.SerializeObject(new { response = new { agent = "x", port = "y", error = "z" }});
}
else
{
return JsonConvert.SerializeObject(new { response = "ok"});
}
More info:
https://msdn.microsoft.com/en-us/library/bb397696.aspx
I personally don't think you need a Response class, especially that it is of object type. IMHO, you've overcomplicated the very simple issue that you have. It is not only the __type, but also other info like HelperErrorResponseClass:#AppCommonLib that isn't supposed to be there.
Another Issue you have is the incorrect name of the HelperErrorResponseClass class. This is not a helper class. It is a standard data-object class.
A helper class is a class filled with static methods. It is usually used to isolate a "useful" algorithm.
This is how I would do it:
I'd get rid of the Response class.
I'd use your original simple HelperErrorResponseClass class, but rename it to something more meaningful like ErrorDetails.
I'd return the response like this:
.
if (errorsList.Count() > 0) {
return JsonConvert.SerializeObject(new { response = errorsList});
}
else {
return JsonConvert.SerializeObject(new { response = "ok"});
}
However, if you really want to stick to your updated solution, an easy way to get rid of the __type is simply removing it from the final serialized string:
if (errorsList.Count() > 0) {
string r = JsonConvert.SerializeObject(new { response = errorsList});
return r.Replace("__type", "");
}
else {
return JsonConvert.SerializeObject(new { response = "ok"});
}

.NET MVC API - dots (period) in get request

I'm trying to setup Facebook Notification API.
I have an APi Controller with RealtimeUpdate() - Get, will be used just for verification of endpoint.
As is written in Fb Docs:
Firstly, Facebook servers will make a single HTTP GET to your callback
URL when you try to add or modify a subscription. A query string will
be appended to your callback URL with the following parameters:
hub.mode - The string "subscribe" is passed in this parameter
hub.challenge - A random string
hub.verify_token - The verify_token value you specified when you created the subscription
From here I have a problem - I have no idea how to handle this dots in query params names. I google a lot, and did not find the solution.
Can somebody please say to me how to get data from this hub.* values?
Thank you!
Update your method signature using the FromUri attributes, like this:
public string Get(
[FromUri(Name="hub.mode")]string mode,
[FromUri(Name="hub.challenge")]string challenge,
[FromUri(Name="hub.verify_token")]string verifyToken
)
{
/* method body */
}
The parameters will be bound from the query string using the specified names.
Slightly different form Steve's answer.
In case you need to have a normal controller instead of an Api one (if you are inheriting from Controller rather tha ApiController), the follow worked for me:
namespace Name
{
public class Hub
{
public string Mode { get; set; }
public string Challenge { get; set; }
// ReSharper disable once InconsistentNaming
public string Verify_Token { get; set; }
}
public class FacebookWebHooksController : Controller
{
[System.Web.Http.HttpGet, ActionName("Callback")]
[AllowAnonymous]
public ContentResult CallbackGet(Hub hub)
{
if (hub.Mode == "subscribe" && hub.Verify_Token == "YOUR_TOKEN")
return Content(hub.Challenge, "text/plain", Encoding.UTF8);
return Content(string.Empty, "text/plain", Encoding.UTF8);
}
}
[HttpPost]
[AllowAnonymous]
public ActionResult Callback()
{
Request.InputStream.Seek(0, SeekOrigin.Begin);
var jsonData = new StreamReader(Request.InputStream).ReadToEnd();
}
}
The Model Binder has some illegal characters, of which I believe '.' is a special character, used primarily to bind complex objects. When all else fails, you can look at Request.QueryString and Request.Form directly, just like in ASP.NET WebForms.
You can also try using a complex object that has a Property named hub with subproperties mode, challenge, and verify_token. This might just do the trick.

Categories