Monday, May 20, 2024
34
rated 0 times [  36] [ 2]  / answers: 1 / hits: 33586  / 11 Years ago, mon, june 3, 2013, 12:00:00

I am using swagger with servicestack but I am getting a 401 unauthorised error from my /resources URL becuase it requires an API key.



Unless I'm mistaken, according to the documentation I should set supportHeaderParams to true as well as the apiKeyName and apiKey value in the JSON parameters when initializing Swagger from my html page.



I was then expecting to see my API key in the http request headers, but it is still being appended to the URL and not in the headers collection.



Here is the code that initialises Swagger in my HTML page:



 window.swaggerUi = new SwaggerUi({
discoveryUrl: http://pathtomyservice.com/resources,
headers: { testheader : 123 },
apiKey: 123,
apiKeyName: Api-Key,
dom_id:swagger-ui-container,
supportHeaderParams: true,
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){
if(console) {
console.log(Loaded SwaggerUI);
console.log(swaggerApi);
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
},
onFailure: function(data) {
if(console) {
console.log(Unable to Load SwaggerUI);
console.log(data);
}
},
docExpansion: none
});


Unfortunately I get no headers at all, no 'Api-Key' or 'testheader'.


More From » servicestack

 Answers
181

I think that it might be a bug in swagger ui.



As a workaround, I added the following in in the swagger index.html file.



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


Hope this helps,


[#77847] Saturday, June 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
milor

Total Points: 284
Total Questions: 93
Total Answers: 115

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;