FromBody value get null

16,922

Solution 1

The POST body payload in Fiddler should be:

=foo_bar

instead of:

value=foo_bar

That's just one of those strange things about the model binding in the Web API. If you want to support value=foo_bar in the POST body payload you could always write a view model:

public class MyViewModel
{
    public string Value { get; set; }
}

and then have your method take this view model as parameter:

public void Post(MyViewModel model)
{
    ... work with model.Value here as usual
}

Solution 2

I meet this issue, and the solution is:
to fit the Request Body format =foo_bar,
also need Request haders:

Content-Type: application/x-www-form-urlencoded
Share:
16,922
Hakan Ertuğ
Author by

Hakan Ertuğ

Updated on July 27, 2022

Comments

  • Hakan Ertuğ
    Hakan Ertuğ almost 2 years

    This is Asp.Net Webform application
    This is my POST method in my Apicontroller

    public void Post([FromBody]string value)
    {
    }
    

    I'm with fiddler post process.
    I did so experiment.
    But it did not.

    What is the problem.
    Can you help?

    I've tried it, I've failed.

    public void Post(MyViewModel model)
    {
       string aa = model.Value;
    }
    
    public class MyViewModel
    {
       public string Value { get; set; }
    }
    

    In Fiddler:

    Request Body:   
    Value=hakan
    
  • Hakan Ertuğ
    Hakan Ertuğ almost 11 years
    Edited my question. Can you check?
  • Darin Dimitrov
    Darin Dimitrov almost 11 years
    Did you set the Content-Type request header to application/x-www-form-urlencoded in Fiddler?
  • Hakan Ertuğ
    Hakan Ertuğ almost 11 years
    :):):) I am grateful to you. Thank you very, very much. its working.
  • Jon Onstott
    Jon Onstott almost 11 years
    I was having the same problem with a Windows Java program (using Scala and Dispatch). This answer solved it for me also.
  • Paul George
    Paul George over 9 years
    Thanks - adding "Content-Type: application/x-www-form-urlencoded" worked for me.
  • ANJYR
    ANJYR over 8 years
    Thanks @DarinDimitrov after changed of Content-Type application/x-www-form-urlencoded" it's worked..