POST to ASP.NET WebAPI using Fiddler2

2019-08-27 15:36发布

I have a class that models exactly the entity I have in the database. I have a stored procedure that takes in parameters for a new row and returns all the settings in the table which in turn populates my repository. I am able to see the results of GET, PUT and DELETE in the List of type Setting that is in memory. I am noticing first that even when I close Visual Studio and reopen and run the project, sometimes, the List is still in the state it was before. It is not repopulating from the database so I'm not sure why that is first of all... Secondly, I can't seem to get POST to work from Fiddler unlike the other HTTP verbs. I DO see the values from Fiddler show up in the code below but I get the error: Invalid URI: The format of the URI could not be determined. I get the same error if I pass an ID or not.

Here is what I put into Fiddler:

POST localhost:54852/api/settings

Request Headers

User-Agent: Fiddler
Content-type: application/x-www-form-urlencoded
Host: localhost:54852
Content-Length: 149

Request Body

ID=0&Category=Dried%20Goods&Sub_Category=Other&UnitSize=99&UnitOfMeasureID=999&Facings=true&Quantity=true&EverydayPrice=999.99&PromotionPrice=111.11

PostSetting function within my SettingsController

     public HttpResponseMessage PostSetting(Setting item)
    {
        item = repository.Add(item);            
        var response = new HttpResponseMessage<Setting>(item) { StatusCode = HttpStatusCode.Created };
        string uri = Url.Route("DefaultApi", new { id = item.ID });
        response.Headers.Location = new Uri(uri);
        return response;
    }

Should I create a new procedure that gets the MAXID from the database and use that as the NEW ID in the line above where a new ID is created?

2条回答
We Are One
2楼-- · 2019-08-27 16:24

You need to create a JSON representation of the Setting class or item that you are wanting to test with use Fiddler (now a Telerik product) and use the Composer tab.

Next you will want to perform a POST to the following URL:

http://[your base url]/api/settings

and pass the JSON formatted setting class.

You can see an example of this here: ASP.NET Web API - Scott Hanselman

查看更多
三岁会撩人
3楼-- · 2019-08-27 16:25

Here is a short video showing how to achieve it easily

get and post to webapi from fiddler

enter image description here

查看更多
登录 后发表回答