Reputation: 458
I'm doing a web API in c# with token, but I need to receive the parameters of postman to my method
[HttpPost, Route("api/cobro/saveEmail")]
public IHttpActionResult SaveEmailForDiscount([FromBody] string email)
{
//do something with e-mail
return Ok(email);
}
But always the email is null
This is the PostMan request
POST /api/cobro/saveEmail HTTP/1.1
Host: localhost:53107
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 881045b2-0f08-56ac-d345-ffe2f8f87f5e
email=jose%40gm.com
This is my Startup class where is all config
using System;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Security.OAuth;
using System.Web.Http;
using System.Net.Http.Headers;
[assembly: OwinStartup(typeof(Cobros_BackEnd.Startup))]
namespace Cobros_BackEnd
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
var MyProvider = new AuthorizationServerProvider();
OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = MyProvider
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("text/xml"));
//get all users
config.Routes.MapHttpRoute(
name: "Users",
routeTemplate: "api/{controller}/users",
defaults: new { id = RouteParameter.Optional }
);
WebApiConfig.Register(config);
}
}
}
I'm using other methods in GET and all work fine, but I need to use this on POST
Upvotes: 1
Views: 1271
Reputation: 54
you can create a class wrapper email field like this
public class SaveEmailModel{
public string Email{get;set;}
}
public IHttpActionResult SaveEmailForDiscount([FromBody] SaveEmailModel model){
...
}
Upvotes: 2
Reputation: 812
try this in the body of your request: =jose%40gm.com
Web Api does not play nice with simple types being passed in the body of a post. You need to actually implement a custom data binder to be able to do this. What I just put is a work around. I avoid posting simple types in body at all costs in Web api. Id prefer to make a Model object and then send the data in JSON which will map to my model. You can also use [FromUri] and pass your string inside the url.
Upvotes: 2