1

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

0

2 Answers 2

2

you can create a class wrapper email field like this

public class SaveEmailModel{
    public string Email{get;set;}
}

public IHttpActionResult SaveEmailForDiscount([FromBody] SaveEmailModel model){
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the easiest solution to this problem IMO
2

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.

1 Comment

Can you give an example or a reference?

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.