8 Replies - 1669 Views - Last Post: 13 June 2011 - 11:17 AM Rate Topic: -----

#1 thursdayniac  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 255
  • Joined: 26-April 09

Serialize and send object using REST

Posted 13 June 2011 - 08:56 AM

Hi all,

I'm trying to set up a REST service that simply serialize a simple object(this object just holds a few serializable strings) and send it back as a response to a rest call.

I cannot seem to find any good examples online.
So right now I have a method called InvokeService, which executes when it receives a REST call. Can anyone point me in the right direction/have a link to a good example of how to serialize and respond with an object? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Serialize and send object using REST

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Serialize and send object using REST

Posted 13 June 2011 - 09:05 AM

Or is this an ASP.NET application? Or something else?

Anyway, you either need to mark your object as Serializable and use an XmlSerializer, or if it's really simple, just add a method to it that creates the XML yourself.

As to how to return that data, that depends on what kind of project this is.
Was This Post Helpful? 0
  • +
  • -

#3 thursdayniac  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 255
  • Joined: 26-April 09

Re: Serialize and send object using REST

Posted 13 June 2011 - 09:30 AM

It's a windows forms applications

But the REST service code is in a web form
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,212
  • Joined: 23-August 08

Re: Serialize and send object using REST

Posted 13 June 2011 - 09:32 AM

Have you tried typing WCF REST into a search engine?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Serialize and send object using REST

Posted 13 June 2011 - 09:44 AM

To be fair, not everyone knows what WCF even is, so they wouldn't know to search on it.

Anyway, I'm confused. Which application are you asking the question about? Which one is the InvokeService in? Which one is the one doing request, and response? Your question is pretty unclear.

For the record, this is how it actually works:

Server Client
------------------
ASP.NET WinForms.

The server handles and responds to requests made by the client.

There's nothing special about REST. It's basically just a normal web response. But instead of HTML, you respond with XML or JSON, or whatever other format that is specified.
Was This Post Helpful? 1
  • +
  • -

#6 thursdayniac  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 255
  • Joined: 26-April 09

Re: Serialize and send object using REST

Posted 13 June 2011 - 10:39 AM

My main question is just how to return a response from a REST call.

For intance, I have this InvokeRequest() function in a web form code behind called CheckAgreement.aspx.cs. But how to I return the XML data? is it the return type of the function?
Do I just return an XML string?

Another way of putting my question:
when I call request.GetResponse from my code that is making the REST call, where does this response come from(in the web service code)?

I have seen plenty of examples of doing simple C# REST calls, but I have yet to see an example of the code that generates the response. That's all I'm looking for. Is this too vague?
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Serialize and send object using REST

Posted 13 June 2011 - 10:59 AM

In standard ASP.NET, you can just Response.Write the XML, then Response.End .

It's really no different than requesting a page and receiving HTML, except that instead of responding with HTML, the server responds with XML. Or JSON, which I've started to prefer these days.

So in your InvokeRequest method, you'd build your XML response, and then Response.Write it. At the end, Response.End. Instead of rendering HTML to the client, it will respond with the XML, then quit responding. If you visit the page in the browser, you'll see the XML. That's what you'd receive if you use a WebClient or HttpWebRequest to get the value in code.
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,212
  • Joined: 23-August 08

Re: Serialize and send object using REST

Posted 13 June 2011 - 11:14 AM

True, I made a big giant assumption. It is rather simple to do this via WCF though.
Was This Post Helpful? 1
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Serialize and send object using REST

Posted 13 June 2011 - 11:17 AM

Also ASP.NET MVC3. especially JSON. I hate to keep harping on JSON, but they've got a return type called JsonResponse, which can basically reflect an object to be serialized into JSON and return it as the controller's response. Each method acts as an endpoint, and you can mark which verbs they'll respond to, like [HttpGet], [HttpPost], etc.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1