2

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":"webapi@restserver.com","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":"webapi@restserver.com","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

bryanmac
  • 38,941
  • 11
  • 91
  • 99
Dean Blakely
  • 3,535
  • 11
  • 51
  • 83

1 Answers1

3

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

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • My question was "how can I put a json string in an HTTP URL." Might you have an answer to that question? I'm having a heck of a time getting an answer to it. Your answer here is interesting and related to my other post: http://forums.asp.net/t/1843826.aspx/1?Web+api+CRUD+Operations . In a nuthull the world is more complex that get, put, and post. I'm mirroring my WCF services that do all of that and more in a single call. – Dean Blakely Sep 26 '12 at 15:57
  • The reason you're having a hard time getting an answer is it's not the right way to go about a REST service and ends up being problematic (max url length, encoding data etc...). The right way is to pass structured REST data in the body. – bryanmac Sep 27 '12 at 17:14
  • I now know how to do, for instance, a post with a json string in its Entity param. If my asp.net web api can return another json string from that post then I can use post for everything. All I want to do is pass large complex data and get back large complex data. Do you think the post will do that? Thanks – Dean Blakely Sep 27 '12 at 17:22
  • POST has a response and a response has a body as well. So, you could post complex JSON in the body of the POST and then the response would be a 200 with the data in body. Another pattern I've seen is an url in the body of the response referring to some other complex data for the client to get but that adds another request. – bryanmac Sep 27 '12 at 18:59