I used the starter kit to begin with. Adding references to the dll's in the starter kit (like Microsoft.Http.dll) and code like this (sample from the starter kit) worked well
using System.IO; using Microsoft.Http; using System.Net; using System.Xml;
protected void consumeRest() { HttpClient http = new HttpClient("http://twitter.com/statuses/"); http.TransportSettings.Credentials = new NetworkCredential("{username}", "{password}"); HttpResponseMessage resp = http.Get("friends_timeline.xml"); resp.EnsureStatusIsSuccessful(); ProcessStatuses(resp.Content.ReadAsStream()); } static void ProcessStatuses(Stream str) { XmlDocument doc = new XmlDocument(); doc.Load(str); XmlNodeList statuses = doc.SelectNodes("/statuses/status"); foreach (XmlNode n in statuses) Console.WriteLine("{0}: {1}", n.SelectSingleNode("user/screen_name").InnerText, n.SelectSingleNode("text").InnerText); }
If you just want to read an external webservice such as an RSS or ATOM feed from a blog, Flickr, sports scores, a weather service, stock price service, etc. etc. you can use the built-in umbraco.library:GetXlmDocumentByUrl() function. There is even an optional cache period to minimize calls and help isolate you from performance latency on the serving site. It's even available from within XSLT macros!
If you want to make your site's content a service, you might look at the RSS feed xslt template and use a custom template with a macro to output the correct format.
Thanks alot, I was going the GetXmlDocumentByUrl first, but the service Im connecting to requires content+type xml for the request to be handled. iNow I guess the easiest way would have been to copy that function from library source, and add a optional contenttype param. Instead I was lost in the dungeons of the ms starter kit. Well, Iearned somethings at least. And to create my own rest+services I will surely go for base. Btw + are there limits on the data+length posting data to -base I need to post a big form, for another function Iäm inplementing,
If you need your pages to return as text/xml you can use umbraco.library.ChangeContentType, if you're using an xslt file to generate the content put this snippet right inside the opening of the xsl:template.
Regarding /base I think you can only post via the url sent, and I haven't seen it used with POST requests, so I think your options are fairly limited in that respect.
Hi Jonas, actually /base handles POST request just fine. So, your only limitation is the POST protocol. Which, I don't think has a set limit really (think of uploading files). So, you could use jQuery to post the data to your base URL.
That's right, using those options will make the whole page xml. I thought it was your site that was providing the REST data.
As I understand it you're trying to consume data from an external service that's providing data via an text/xml request - which in my ears sounds like some kind of webservice. In that case you'll probably have to look into creating some kind of usercontrol that calls the webservice from server-side code, or you could check if the service perhaps provides JSON data via an AJAX call.
Ah, ok Nik, I'll try that, thanks! But, um, how about non-javascripters? A plain form post would work for them, right? <form action="{url to base}" method="post">
I saw my typing was worse than ever in a post here. It was due to I cannot add posts in my iphone browser (anyone?), so I opened a rdp-client on my phone adding the post that way... Worked, but not without alot of typos... "Who said you couldn't" right? :)
Jesper, yes, indeed a web service, and perhaps my code was not such a overkill then after all, using HttpClient.dll, however the addition of yet another of those dll's in the bin.
I am creating a WCF restfull service in Umbraco. I have some content like category and products that i want to expose through restful url. the problem i am facing is WCF is deployed in umbraco successfully but when i tried to execute some code from umbraco dll i gave me "Object reference not set to an instance of an object." error. i tried the code by making a simple asmx web service and its execute fine but when i tried with WCF i got the error.
Consuming Rest within Umbraco
Hi!
Are there any Rest client-functionality in the Umbraco API or would I best go to the Rest Starter Kit using the HttpClient for that? http://www.asp.net/downloads/starter-kits/wcf-rest/
Regards
Jonas
I used the starter kit to begin with. Adding references to the dll's in the starter kit (like Microsoft.Http.dll) and code like this (sample from the starter kit) worked well
The /base functionality in Umbraco can be used to provide som REST like features. Check out the wiki at: http://our.umbraco.org/wiki/reference/umbraco-base
It's very simple to setup and use, so if you only require REST for a limited set of pages/features consider using /base.
Regards
Jesper Hauge
If you just want to read an external webservice such as an RSS or ATOM feed from a blog, Flickr, sports scores, a weather service, stock price service, etc. etc. you can use the built-in umbraco.library:GetXlmDocumentByUrl() function. There is even an optional cache period to minimize calls and help isolate you from performance latency on the serving site. It's even available from within XSLT macros!
If you want to make your site's content a service, you might look at the RSS feed xslt template and use a custom template with a macro to output the correct format.
Or use /base, which is awesome.
cheers,
doug.
Jonas,
If you want good example on how to use umbraco.library:GetXmlDocumentByUrl() take a look at the twitter package created by Warren Buckley
Regards
Ismail
Thanks alot, I was going the GetXmlDocumentByUrl first, but the service Im connecting to requires content+type xml for the request to be handled. iNow I guess the easiest way would have been to copy that function from library source, and add a optional contenttype param. Instead I was lost in the dungeons of the ms starter kit. Well, Iearned somethings at least. And to create my own rest+services I will surely go for base. Btw + are there limits on the data+length posting data to -base I need to post a big form, for another function Iäm inplementing,
Regards
Jonas
Hi Jonas
If you need your pages to return as text/xml you can use umbraco.library.ChangeContentType, if you're using an xslt file to generate the content put this snippet right inside the opening of the xsl:template.
Or if you are using masterpages you can set the content type from code in that as well:
Regarding /base I think you can only post via the url sent, and I haven't seen it used with POST requests, so I think your options are fairly limited in that respect.
Regards
Hauge
Hi Jesper, I dont want the whole page to be xml, just the request for the data from that service, ChageContentType will change the whole page, no?
Hi Jonas, actually /base handles POST request just fine. So, your only limitation is the POST protocol. Which, I don't think has a set limit really (think of uploading files). So, you could use jQuery to post the data to your base URL.
HTH!
-- Nik
That's right, using those options will make the whole page xml. I thought it was your site that was providing the REST data.
As I understand it you're trying to consume data from an external service that's providing data via an text/xml request - which in my ears sounds like some kind of webservice. In that case you'll probably have to look into creating some kind of usercontrol that calls the webservice from server-side code, or you could check if the service perhaps provides JSON data via an AJAX call.
Regards
Jesper Hauge
Ah, ok Nik, I'll try that, thanks! But, um, how about non-javascripters? A plain form post would work for them, right? <form action="{url to base}" method="post">
I saw my typing was worse than ever in a post here. It was due to I cannot add posts in my iphone browser (anyone?), so I opened a rdp-client on my phone adding the post that way... Worked, but not without alot of typos... "Who said you couldn't" right? :)
Jesper, yes, indeed a web service, and perhaps my code was not such a overkill then after all, using HttpClient.dll, however the addition of yet another of those dll's in the bin.
Regards / Jonas
1) Actually there's really good umbraco tv clip about posting to /Base.
2) But yes you can post. Something like the following
public static void Login()
{
var userName = HttpContext.Current.Request.Form["userName"];
var password = HttpContext.Current.Request.Form["password"];
// some logic
}
3) Check out http://jquery.malsup.com/form/ it's pretty clever. If the user does not have js enabled it post the form without ajax.
Hi All,
I am creating a WCF restfull service in Umbraco. I have some content like category and products that i want to expose through restful url. the problem i am facing is WCF is deployed in umbraco successfully but when i tried to execute some code from umbraco dll i gave me "Object reference not set to an instance of an object." error. i tried the code by making a simple asmx web service and its execute fine but when i tried with WCF i got the error.
Can anyone help me out what can be the problem.
Regards/ Nayyar
is working on a reply...