Ilya Rogojin
Ilya Rogojin

Reputation: 321

Azure: is it possible to create rest service without virtual machine?

Is there any opportunity to create a custom REST service without creating a virtual machine (no matter, some other server with software to work directly with azure SQL) to serve that.

Situation:

I want to call this procedures as a REST service, sending them a parameters (string, int) and get a result in JSON format. Also I'm interested in access control for such kind of service.

I can create all that stuff using my own software (connecting to Azure SQL), but is there such opportunity in azure itself?

I don't need full solution, I need an answer if is it possible and what kind of solutions(resources) on azure should I use. I've already read a lot, but I still can't find a solution.

rem: I need Azure because of using it's storage and analytics engines in project.

Upvotes: 0

Views: 123

Answers (3)

Thiago Custodio
Thiago Custodio

Reputation: 18387

I believe the easiest way is using Azure Functions with Http Trigger template. The only thing you'll need to do is extract the variables from content or querystring:

using System.Net;
using System.Threading.Tasks;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    name = name ?? data?.name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

more info: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook

Upvotes: 2

Jovan MSFT
Jovan MSFT

Reputation: 14640

I would definitely recommend Azure functions for this. In the previous answers you can see how to generate response, and here is example how to call stored procedure - https://www.codeproject.com/Articles/1169531/Sending-events-from-Azure-Event-Hub-to-Azure-SQL-D

Azure SQL has FOR JSON clause that can transform results of sql query to JSON text so you can directly stream it as a Http response.

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72191

Azure WebApp or Azure Functions would be the way to go for you, they offer the ability to run code without having to manage the underlying structure. Depending on your scenario you might settle with one or the other.

Upvotes: 4

Related Questions