Search code examples
androidjsonrestasp.net-web-apirestful-architecture

sending a json string in a http url


I'm sending data from my Android app to my REST Service written as a asp.net web api. I am already successfully sending urls such as

http://www.deanblakely.com/REST/api/products/3 and processing the params in the web.api.

Now, in the Android client I have used GSON to convert a complex object to a json string and the string is as follows:

[{"TStamp":"Sep 25, 2012 5:04:46 PM","emailaddress":"[email protected]","altitude":0.0,"latitude":3.3743984E7,"longitude":-1.18107841E8,"legnum":1}]

How do I put this json string into the url? for instance can I just put it between the slashes? such as http://www.deanblakely.com/REST/api/objects/{"TStamp":"Sep 25, 2012 5:04:46 PM","emailaddress":"[email protected]","altitude":0.0,"latitude":3.3743984E7,"longitude":-1.18107841E8,"legnum":1} ??

PS. all my methods at the web.api are Gets. I want to keep it that way because it's simpler. If you are doubting this strategy please see: http://forums.asp.net/t/1843826.aspx/1?Web+api+CRUD+Operations

Thanks, Dean


Solution

  • For RESTful web apis, a get should not modify state or resources on the server.

    Putting the params in the query string URL is typically done for a GET request of a REST resource.

    As you can see by your question, doing everything via a GET is not simpler. It's not typical and introduces many problems when you attempt to post more complex objects and graphs through a querystring which is more appropriate as filter options on a GET.

    If you are sending a JSON representation of an object like that to a server web api, you are typically doing a create or update. It's easier to serialize it to JSON and place it in the body.

    If you are doing a creation of a resource, then do a POST and put the JSON object in the body of the request.

    If you are doing an update, (likely since you have /3), then do a POST, PUT or a PATCH request to the url and put the json object in the body. A PUT implies a complete replacement of the resource as opposed to a PATCH which is a partial update.

    Since you're using ASP.net web api, here's a walk through on using with CRUD operations. Go through it with fiddler running:

    http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations