0

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Shane, as you can see above, that is what I did. I figured this out several days ago after trying a bunch of different things. What I needed to do was two things. 1.) Since I was using Basic Authentication I needed to add a Base64 encoded string. 2.) I needed to change the Content-Type: text/json. My Request Body the following example {"ID":123,"CatalogID":9876,"Category":"Fruit"...}
Thanks for the video, I watched those before I asked this question.
0

Here is a short video showing how to achieve it easily

get and post to webapi from fiddler

enter image description here

Comments

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.