What is api_key and how to use it correctly?

I'm new to quiet services, and I just implemented a test code to get a ServiceStack service that works with the Swagger plugin, which also fails me ...

inside swagger-ui / index.html there is a field for 'api_key'. I know that the variable name is umm ... a variable, and I can set it too as I like, but I'm a little confused about what it is used for and whether I should use it.

Also, if I use it, how does servicestack represent this value for me on the server side?

Here is the test service I got from the documentation ...

[Api("Hello Web Services")] [Route("/Hello", Summary = @"Noel ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes")] [Route("/Hello/{name}", Summary = @"N031'5 ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes", Verbs="GET,POST" )] public class Hello { [ApiMember(Name = "Name", Description = "This is a description", ParameterType = "path", DataType = "string", Verb="GET,POST")] public string Name { get; set; } } public class HelloResponse { public string Result { get; set; } } public class HelloService : Service { public object Any(Hello request) { return new HelloResponse { Result = "Hello, " + request.Name }; } } 
+4
source share
2 answers

to answer my own Esker request, here's how to use the API key ...

 public class HelloService : Service { public object Any(Hello request) { string api_key = this.Request.Headers["api_key"]; return new HelloResponse { Result = "Hello, " + request.Name }; } } 

but some additional javascript is also needed to include it in a header like this (inside swagger-ui / index.html) ...

  $(function () { $.ajaxSetup({ beforeSend: function (jqXHR, settings) { jqXHR.setRequestHeader("api_key", $("#input_apiKey").val()); } }); }); 

which I found in the answer in this question ...

How to get Swagger to send the API key as http, not a URL

+4
source

The Swagger interface has a general concept of providing api_key for use in every request sent to your service described here . It can be sent as a query string or a header value, and you can also change the parameter name as described in the link above.

You only need to configure this if you really need the API key in the ServiceStack service (for example, if you have a request filter that validates and validates the API key, perhaps).

The reason you might need to set the default API key value in your JavaScript code to configure Swagger instead of entering the user type in the API key is because as soon as the index.html page loads, it will send a few requests to your a service for retrieving metadata, so he wants to know what default API key value to send for these metadata requests before the user can start interacting.

+3
source

Source: https://habr.com/ru/post/1496746/


All Articles