ASP.NET web api cannot get application/x-www-form-urlencoded HTTP POST

前端 未结 6 626
无人共我
无人共我 2020-12-01 10:53

I\'m new to web-api. I want to receive a HTTP POST data using web-api. The content-type is application/x-www-form-urlencoded, and the request body is like:

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 11:20

    This post is old, but I stumbled on it while searching for answer. I'll post how I got mine to work, maybe someone will find it useful.

    Here's the request:

    POST /api/values HTTP/1.1
    Host: localhost:62798
    Accept: text/json
    Content-Type: application/x-www-form-urlencoded
    Cache-Control: no-cache
    Postman-Token: 51ee1c5f-acbb-335b-35d9-d2b8e62abc74
    
    UID=200&EMAIL=john%40jones.com&FIRST_NAME=John&LAST_NAME=jones&PHONE=433-394-3324&CITY=Seattle&STATE_CODE=WA&ZIP=98105
    

    Here's the Model:

    public class SampleModel{
        public string UID { get; set; }
    
        public string Email { get; set; }
    
        public string First_Name { get; set; }
    
        public string Last_Name { get; set; }
    
        public string Phone { get; set; }
    
        public string City { get; set; }
    
        public string State_Code { get; set; }
    
        public string Zip { get; set; }
    }
    

    And here's the POST method that automagically(FromBody) converts urlencoded values to the model.

    public HttpResponseMessage Post([FromBody] SampleModel value){
    

    I was able to pick out any value i.e.

        SearchCity(value.City);
        SearchName(value.Last_Name);
    

提交回复
热议问题