Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the GET Query Parameters in a simple way on Azure Functions C#?

I tried the following:

/// <summary>
/// Request the Facebook Token
/// </summary>
[FunctionName("SolicitaFacebookToken")]
[Route("SolicitaToken/?fbAppID={fbAppID}&fbCode={fbCode}&fbAppSecret={fbAppSecret}")]
public static async Task<HttpResponseMessage> SolicitaFacebookToken(
    [HttpTrigger(AuthorizationLevel.Function, methods: new string[] { "get" } )]
    HttpRequestMessage req,
    TraceWriter log,
    string fbAppID,
    string fbCode,
    string fbAppSecret
)
{ }

When I access the URL:

http://localhost:7071/api/SolicitaFacebookToken/?fbAppID=ABC&fbCode=DEF&fbAppSecret=GHI

But it gives these errors:

'SolicitaFacebookToken' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?
System.InvalidOperationException : 'SolicitaFacebookToken' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK
attributes?
 at Microsoft.Azure.WebJobs.JobHost.Validate(IFunctionDefinition function,Object key)
 at async Microsoft.Azure.WebJobs.JobHost.CallAsync(??)
 at async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary\`2 arguments,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.Host.FunctionRequestInvoker.ProcessRequestAsync(HttpRequestMessage request,CancellationToken cancellationToken,WebScriptHostManager scriptHostManager,WebHookReceiverManager webHookReceiverManager)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.<>c__DisplayClass3_0.<ExecuteAsync>b__0(??)
 at async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync(HttpRequestMessage request,Func`3 processRequestHandler,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)
 at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
 at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)

If I change to this:


HttpRequestMessage req,
string fbAppID,
string fbCode,
string fbAppSecret,
TraceWriter log

The following 1 functions are in error:

SolicitaFacebookToken: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1.SolicitaFacebookToken'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'fbAppID' to type String. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

In Azure Functions template code, there is

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

I would like a simpler way to get the GET query parameters.

I want to have a URL like this:

http://localhost:7071/api/SolicitaFacebookToken/?fbAppID=123&fbCode=456&fbAppSecret=789

Q: How can I easily get the parameters and its values?

like image 665
Tony Avatar asked Apr 14 '18 15:04

Tony


People also ask

CAN GET method have query parameters?

When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.

How will you get the query parameter search?

In the View column, click View Settings. Under Site Search Settings, set Site Search Tracking to ON. In the Query Parameter field, enter the word or words that designate internal query parameters, such as term,search,query,keywords. Sometimes query parameters are designated by just a letter, such as s or q.

How do I give a parameter query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

How do I pass the function key to Azure function?

Get the function's master keyNavigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.


2 Answers

In the past when I have multiple parameters I've added them to the route. So instead of this:

[Route("SolicitaToken/?fbAppID={fbAppID}&fbCode={fbCode}&fbAppSecret={fbAppSecret}")]

I've done something like this:

[Route("SolicitaToken/{fbAppID}/{fbCode}/{fbAppSecret}")]

Then you don't need to access the query string at all and can use the function parameters directly.

[FunctionName("Function1")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "HttpTriggerCSharp/name/{fbAppID}/{fbCode}/{fbAppSecret}")]HttpRequestMessage req, string fbAppID, string fbCode, string fbAppSecret, TraceWriter log)
{
  log.Info("C# HTTP trigger function processed a request.");
  var msg = $"App ID: {fbAppID}, Code: {fbCode}, Secret: {fbAppSecret}";
  // Fetching the name from the path parameter in the request URL
  return req.CreateResponse(HttpStatusCode.OK, msg);
}
like image 61
Sean Avatar answered Sep 21 '22 08:09

Sean


For v2/beta/.NET Core runtime, you can be specific and do:

string fbAppID = req.Query["fbAppID"];

or more generic with:

using System.Collections.Generic;
...
IDictionary<string, string> queryParams = req.GetQueryParameterDictionary();
// Use queryParams["fbAppID"] to read keys from the dictionary.

For v1 function apps (.NET Full Framework):

using System.Collections.Generic;
...
IDictionary<string, string> queryParams = req.GetQueryNameValuePairs()
    .ToDictionary(x => x.Key, x => x.Value);
// Use queryParams["fbAppID"] to read keys from the dictionary.
like image 23
evilSnobu Avatar answered Sep 19 '22 08:09

evilSnobu