0

I used the Spring REST service implementation to learn Swagger.

Service on GET - http://localhost:8080/greeting?name=Betlista returns

{
  "id":5,
   "content":"Hello, Betlista!"
}

So I created YAML file for Swagger as:

swagger: "2.0"
info:
  version: "0.0.1-SNAPSHOT"
  title: "Spring REST"
host: "localhost:8080"
basePath: "/"
tags:
- name: "Greeting"
schemes:
- "http"
paths:
  /greeting:
    get:
      operationId: "greeting"
      produces:
      - "application/json"
      parameters:
      - name: "name"
        in: "query"
        required: false
        type: "string"
      responses:
        "200":
          description: "successful operation"
          schema:
            $ref: "#/definitions/GreetingResponse"
definitions:
  GreetingResponse:
    type: "object"
    properties:
      id:
        type: "integer"
      content:
        type: "string"

The problem is, when I tried to execute it using "Try it out" button.

It seems (from Chrome's Network tab in developer tools) like there is no response:

Not working call from Swagger editor

...while regular call in browser works fine

Working from Chrome

edit: as I mentioned in comments, I verified curl generated by swagger and it works as expected - curl -X GET "http://localhost:8080/greeting?name=Betlista" -H "accept: application/json"

At the moment I'm looking into CORS topic (which makes no sense to me as documentation and service are both on localhost), I see in Chrome console this:

CORS error

Betlista
  • 9,561
  • 10
  • 62
  • 99
  • 1
    Does this answer your question? [Swagger Editor shows "Failed to fetch" error](https://stackoverflow.com/questions/43375605/swagger-editor-shows-failed-to-fetch-error) – Rob Evans Nov 02 '20 at 13:05
  • @RobEvans Thanks, I'll check and let you know! I can see at first sight that I have there that `Failed to fetch`. – Betlista Nov 02 '20 at 13:07
  • seems likely swagger has console errors if the requests are otherwise identical and one is failing and the plain Chrome request is not. If you try curl as well, and that works, its almost certainly swagger thats broken, not the endpoint/request – Rob Evans Nov 02 '20 at 13:12
  • @RobEvans I checked and it's not solving my problem - there is no authorization, but in console I can see, thank you, `swagger-ui.js:1 GET http://localhost:8080/greeting?name=Betlista net::ERR_FAILED` I'll try Google for the same... – Betlista Nov 02 '20 at 13:13
  • @RobEvans I tried `curl`, that worked well... – Betlista Nov 02 '20 at 13:14
  • can you supply the code for `"#/definitions/GreetingResponse"` maybe its a parsing error? – Rob Evans Nov 02 '20 at 13:26

1 Answers1

0

This is in fact problem of the service, not of the Swagger Editor.

I'm still looking for a solution, which is described here:

CORS uses special HTTP headers to allow cross-domain requests. The "try it out" feature requires the following headers in API responses:

Access-Control-Allow-Origin: https://host.from.which.the.request.came
Vary: Origin
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: ResponseHeader1, ResponseHeader2, ...

I tried to use Spring REST sevice with CORS, which on curl -I http://localhost:8080/greeting?name=Betlista returns:

HTTP/1.1 200
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Content-Length: 37
Date: Mon, 02 Nov 2020 13:42:15 GMT

but this is still not working...

As I said, it's not the Swagger Editor problem, but service problem.

CORS check can be turned off in Chrome starting it chrome.exe --disable-web-security --user-data-dir=/tmp as described here which is of course not recommended, so I consider this as a workaround only.

What I used at the end was this - https://stackoverflow.com/a/47022289/384674

edit: later I used - https://stackoverflow.com/a/40300363/384674

Betlista
  • 9,561
  • 10
  • 62
  • 99