I have a single backend defined, for this example named myserver with a runtime url https://myserver.com. Also, I have "Authorization credentials" set for this backend:
The header x-custom-header is added with a secret value. Every request needs to provide this header.
My backend provides the following endpoints:
GET https://myserver.com/Devices
GET https://myserver.com/Devices/device-1
POST https://myserver.com/Devices
Get https://myserver.com/Weather?location=Vienna
...
Here comes the problem:
I want to provide multiple APIs specific for the data:
Devices Api with API URL suffix Devices:
https://mycompany-apim.azure-api.net/Devices
Weather Api with API URL suffix Weather:
https://mycompany-apim.azure-api.net/Weather
For some reason, I don't understand how I can achieve that with only a single backend without too much configuration hassle. I have configured <set-backend-service backend-id="myserver" /> but that obviously does not correctly resolve https://mycompany-apim.azure-api.net/Devices to GET https://myserver.com/Devices but only to GET https://myserver.com/.
Some solutions that might work:
Define a backend for each API. E.g. backend myserver-devices with the base url https://myserver.com/Devices
Problem: I don't want to have X backends configured the same way just with a different url ending.
Configure an rewrite-uri policy for each endpoint in each API.
Problem: Obviously, this would be even more configuration overhead.
Are there alternatives?
This can be achieved by creating one API e.g. 73219639 and the required operations:
It's important that each APIM operation has the same path e.g. /Devices:
https://rfqapiservicey27itmeb4cf7q.azure-api.net/Devices -> https://myserver.com/Devices
This is the simple working yaml:
openapi: 3.0.1
info:
title: '73219639'
description: ''
version: '1.0'
servers:
- url: 'https://rfqapiservicey27itmeb4cf7q.azure-api.net'
paths:
/Devices:
post:
summary: Devices
description: Devices
operationId: post-devices
responses:
'200':
description: OK
get:
summary: Devices
description: Devices
operationId: get-devices
responses:
'200':
description: OK
/Weather:
get:
summary: Weather
description: Weather
operationId: get-weather
responses:
'200':
description: OK
components:
securitySchemes:
apiKeyHeader:
type: apiKey
name: Ocp-Apim-Subscription-Key
in: header
apiKeyQuery:
type: apiKey
name: subscription-key
in: query
security:
- apiKeyHeader: []
- apiKeyQuery: []
Configure the Backend in the All operations policy with an existing backend:
<policies>
<inbound>
<base />
<set-backend-service backend-id="myserver-com" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
Do not change/modify any other policy. It's working with the default ones.
GET Devices - Test:
HTTP request
GET https://rfqapiservicey27itmeb4cf7q.azure-api.net/Devices HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
Ocp-Apim-Trace: true
Trace: set-backend-service (18.701 ms)
{
"message": "Backend service URL was changed.",
"oldBackendServiceUrl": "https://rfqapiservicey27itmeb4cf7q.azure-api.net/",
"newBackendServiceUrl": "https://myserver.com/",
"request": {
"url": "https://myserver.com/Devices"
}
}
Trace: forward-request (0.127 ms)
{
"message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
"request": {
"method": "GET",
"url": "https://myserver.com/Devices",
"headers": [
{
"name": "Host",
"value": "myserver.com"
},
{
Related
I'm trying to write a yaml that will generate a nodejs stub-server serving a file. Swagger codegen generates server that replies nothing.
swagger: "2.0"
info:
description: ""
version: "1"
title: "Swagger example"
host: "localhost:3200"
basePath: "/v2"
schemes:
- "http"
paths:
/getImage:
get:
tags:
- "image"
summary: "Returns image"
description: ""
operationId: "getImage"
produces:
- "application/octet-stream"
responses:
"200":
description: "successful operation"
schema:
type: "file"
example:
sampleFile:
summary: "A sample file"
externalValue: "http://github.com/topheroes/topheroes.github.io/raw/master/img/dragon.png"
"400":
description: "Invalid value"
Is there a way to generate a stub that will serve a local or a random binary (image) file?
I tried type: string and format: binary, I tried url in example. Nothing seems to work.
Please advice
Generating a project with jhipster#6.2.0 with API-First development and JWT does not send the authorization header.
api.yml (default generated with addition of /api prefix and pet path/schema)
# API-first development with OpenAPI
# This file will be used at compile time to generate Spring-MVC endpoint stubs using openapi-generator
openapi: '3.0.1'
info:
title: 'temp2'
version: 0.0.1
servers:
- url: http://localhost:8080/api
description: Development server
- url: https://localhost:8080/api
description: Development server with TLS Profile
paths:
/pet/findByStatus:
get:
tags:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
responses:
200:
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
400:
description: Invalid status value
content: {}
components:
schemas:
Pet:
required:
- name
- photoUrls
type: object
properties:
id:
type: integer
format: int64
securitySchemes:
jwt:
type: http
description: JWT Authentication
scheme: bearer
bearerFormat: JWT
security:
- jwt: []
./mvnw generate-sources
./mvnw
Visit http://localhost:8080/admin/docs
The authorization header is sent for the account-resources GET /api/account
However it is not sent for the pet request GET /api/pet/findByStatus resulting in a 401 Unauthorized.
In src/main/webapp/swagger-ui/index.html
function addApiKeyAuthorization() {
var authToken = JSON.parse(localStorage.getItem("jhi-authenticationtoken") || sessionStorage.getItem("jhi-authenticationtoken"));
var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + authToken, "header");
window.swaggerUi.api.clientAuthorizations.add("bearer", apiKeyAuth);
}
The clientAuthorization is added with the key "bearer" instead of the autogenerated "jwt".
Changing jwt to bearer resolves it
diff --git a/src/main/resources/swagger/api.yml b/src/main/resources/swagger/api.yml
index b259b3e..1f77650 100644
--- a/src/main/resources/swagger/api.yml
+++ b/src/main/resources/swagger/api.yml
## -42,10 +42,10 ## components:
type: integer
format: int64
securitySchemes:
- jwt:
+ bearer:
type: http
description: JWT Authentication
scheme: bearer
bearerFormat: JWT
security:
- - jwt: []
+ - bearer: []
I have an issue with the embedded API management within a Cloud Foundry application ( node.js ) on bluemix .There is a certain path in the yaml which is not working via the gateway, please see below the relevant path from the yaml:
/socket.io/:
get:
produces:
- text/plain; charset=utf-8
parameters: []
responses:
default:
description: Definition generated from Swagger Inspector
I get 404 , not found.
The url works fine when I dont go via the gateway.
The url is https://[masked api mgd hostname]/socket.io/?EIO=3&transport=polling&t=MC0pE73
Please help.
Find attached the complete yaml below
swagger: "2.0"
info:
description: defaultDescription
version: "0.1"
title: defaultTitle
host: masked.actualEndpoint
schemes:
- https
basePath: "/"
paths:
/socket.io/:
get:
parameters:
- name: t
in: query
required: false
type: string
x-example: MC0pE73
- name: EIO
in: query
required: false
type: string
x-example: "3"
- name: transport
in: query
required: false
type: string
x-example: polling
responses:
default:
description: Definition generated from Swagger Inspector
definitions: {}
am accessing the url using https://[ masked api mangd hostname ]/socket.io/?EIO=3&transport=polling&t=MCvtHJT
I believe its the / at the end of the path ( /socket.io/ ) which is causing the gateway to fail. Any comments.
Thanks for the help in advance.
Currently using cloudformation templates to deploy a simple API to AWS as part of a POC for moving from Azure to AWS API management.
I have got everything working except i have not been able to figure out the YAML AWS extension for setting the HTTP proxy checkbox for the HTTP request.
Sample YAML below. I know this will not set that checkbox (as i have tested it and it worked minus that problem), but on this page
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-integration.html
i cannot see a extension that sets this option? Has AWS not done this yet
AWSTemplateFormatVersion: '2010-09-09'
Resources:
PlayersAPI:
Type: AWS::ApiGateway::RestApi
Properties:
Name: RAH API
Description: A demo API for testing
Body:
swagger: '2.0'
info:
title: test api
description: test api
version: 1.0.1
contact:
name: SH
email: test#mailinator.com
paths:
"/heartbeat":
get:
description: Checks the API is working
produces:
- application/json
responses:
'200':
description: API Response information
x-amazon-apigateway-integration:
type: http
responses:
default:
statusCode: '200'
httpMethod: GET
uri: https://api.example.com
This works for me:
resources:
Resources:
ProxyResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Fn::GetAtt:
- ApiGatewayRestApi # our default Rest API logical ID
- RootResourceId
PathPart: "{proxy+}" # the endpoint in your API that is set as proxy
RestApiId:
Ref: ApiGatewayRestApi
ProxyMethod:
Type: AWS::ApiGateway::Method
Properties:
ResourceId:
Ref: ProxyResource
RestApiId:
Ref: ApiGatewayRestApi
HttpMethod: GET # the method of your proxy. Is it GET or POST or ... ?
MethodResponses:
- StatusCode: 200
Integration:
IntegrationHttpMethod: GET
Type: HTTP_PROXY
Uri: http://bucket.mybucket.co.s3.eu-west-1.amazonaws.com/{proxy} # the URL you want to set a proxy to
IntegrationResponses:
- StatusCode: 200
AuthorizationType: NONE
I have a swagger yaml specification like this :
swagger: "2.0"
info:
version: "0.0.1"
title: Chat API
# during dev, should point to your local machine
host: localhost:5000
# basePath prefixes all resource paths
basePath: /api/v2
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/room:
post:
summary: Get room
operationId: getRoom
tags:
- room
parameters:
-
name: token
in: header
description: "token to be passed as a header"
default: "ZjE4YjMxNmY3OGEzNDMyN2JiYjJmYTQwMDBjODg4OWM="
required: true
-
name: room_id
in: body
description: "get room"
required: true
schema:
$ref: "#/definitions/Room"
definitions:
Room:
required:
- room_id
properties:
room_id:
type: string
This yaml file is compiled well without the header part. If I include the header in the paramerts . The nodejs app keep throwing : "Swagger validation errors"
-
name: token
in: header
description: "token to be passed as a header"
default: "ZjE4YjMxNmY3OGEzNDMyN2JiYjJmYTQwMDBjODg4OWM="
required: true
I don't know what was wrong in this part. I want to add the header to this spec file.
You simply need to add the type attribute. Swagger doesn't know if this is a string, an integer, etc. (although one could say the default explains it).
- name: token
in: header
description: "token to be passed as a header"
default: "ZjE4YjMxNmY3OGEzNDMyN2JiYjJmYTQwMDBjODg4OWM="
required: true
type: string