Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonas Eriksson 930 posts 1825 karma points
    Jan 25, 2010 @ 11:16
    Jonas Eriksson
    0

    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

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 25, 2010 @ 11:32
    Jonas Eriksson
    0

    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);
    }
  • Jesper Hauge 298 posts 487 karma points c-trib
    Jan 25, 2010 @ 14:10
    Jesper Hauge
    1

    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

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jan 25, 2010 @ 14:29
    Douglas Robar
    1

    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.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jan 25, 2010 @ 14:33
    Ismail Mayat
    1

    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

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 25, 2010 @ 16:09
    Jonas Eriksson
    0

    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

     

     

  • Jesper Hauge 298 posts 487 karma points c-trib
    Jan 25, 2010 @ 17:08
    Jesper Hauge
    0

    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.

    <xsl:value-of select="umbraco.library:ChangeContentType('text/xml')" />

    Or if you are using masterpages you can set the content type from code in that as well:

    HttpContext.Current.Response.ContentType = "text/xml";

    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

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 25, 2010 @ 20:23
    Jonas Eriksson
    0

    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?

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jan 25, 2010 @ 20:34
    Nik Wahlberg
    0

    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. 

    $.post(url, {params});

    HTH!

    -- Nik

  • Jesper Hauge 298 posts 487 karma points c-trib
    Jan 25, 2010 @ 20:45
    Jesper Hauge
    0

    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

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 25, 2010 @ 20:49
    Jonas Eriksson
    0

    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? :)

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 25, 2010 @ 20:55
    Jonas Eriksson
    0

    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

  • anthony hall 222 posts 536 karma points
    Jan 25, 2010 @ 21:05
    anthony hall
    0

    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. 

  • Nayyar 1 post 21 karma points
    Jul 11, 2011 @ 05:58
    Nayyar
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft