I wanted to try out Azure Functions to see if I can serve html data from Azure Functions that I get from another third party site. As in, if I use RestSharp and call another site from my Azure Function, extract its content and just display it as output to html. This can come in handy, lets say you have a proxy that is blocking you to get to certain sites and you just want to extract some data from the site and process it. Since Azure function is so cheap, I thought I would give it a try.
Here is a sequence diagram to explain the idea of it.
So the idea is quite simple, you make an Azure Function that can serve you the site html since you are blocked to access it.
We first need to go and create ourselves a HttpTrigger C# function in azure. If you wish to learn more about how to create an HTTP trigger you can read my previous post on Timer Event Trigger which is very similar to http trigger.
First we will need to add a new file called project.json, such that we can add our RestSharp Nuget package.
1 2 3 4 5 6 7 8 9 |
{ "frameworks": { "net46":{ "dependencies": { "RestSharp" : "105.2.3" } } } } |
For our code we will write just a simple GET call with RestSharp and serve it back as a stream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System.Net; using System.Net.Http.Headers; using System.Threading.Tasks; using RestSharp; using System.Text; public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log) { var client = new RestClient("http://www.cbc.ca"); var request = new RestRequest(Method.GET); var res = client.Execute(request); Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(res.Content)); var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return response; } |
You can then just simply view the Url of your azure function by calling it in a browser. Yes the images wont show up property, but all we want is the html to extract some data of, so we are ok that images dont show up properly.
There you have it to serve html data from third party site with Azure Functions.
Leave A Comment