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?

cuongle
  • 74,024
  • 28
  • 151
  • 206
brianhevans
  • 1,183
  • 4
  • 15
  • 28
  • Adding UriKind.Relative like I found in this post > http://stackoverflow.com/questions/9738174/invalid-uri-the-format-of-the-uri-could-not-be-determined-c-sharp < solved my POST issue. I am now able to see my POSTed setting. I still don't know why the local setting is help in memory. Also, I am noticing that the ID column created a new ID despite me providing a new one and that will undoubtedly ruin my PK in the underlying table. Any suggestions on saving this data back to the database? – brianhevans Sep 13 '12 at 19:30
  • See related answer here [http://stackoverflow.com/questions/12321350/webapi-put-call/12571866#12571866][1] [1]: http://stackoverflow.com/questions/12321350/webapi-put-call/12571866#12571866 – Shayne Boyer Sep 25 '12 at 12:30

2 Answers2

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

Shayne Boyer
  • 410
  • 3
  • 9
  • 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"...} – brianhevans Sep 25 '12 at 20:41
  • Thanks for the video, I watched those before I asked this question. – brianhevans Sep 25 '12 at 20:42
0

Here is a short video showing how to achieve it easily

get and post to webapi from fiddler

enter image description here

JuanK
  • 2,056
  • 19
  • 32