How to render the response body in Swagger UI? - swagger-ui

I have a simple GET API. When I click "Try it out" and then "Execute" in Swagger UI, I can see the request reaching the server and getting processed successfully with HTTP status code 200 - ascertained by looking into the server logs.
I would like to see the response body in Swagger UI. What addition or change needs to be done to the OpenAPI YAML spec? I'm using OpenAPI 3.0.3.

By enabling CORS in my flask app, I have managed to overcome the issue ...
From shell prompt ...
pip install -U flask-cors
Inside app.py ...
...
from flask_cors import CORS
...
app = Flask(__name__)
CORS(app)
...

Related

Swagger doesn't found the yaml file (404)

I am using swagger to show my open API YAML file, it suddenly gives me "response status is 404 /openapi.yaml" when I run the spring boot application.
Note : I can open the /api-docs/ in swagger and it shows the apis, also when I hit the swagger config URL (http://localhost:8080/api-docs/swagger-config), it gives me the below response, which include the openapi.yaml file
{"configUrl":"/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8080/swagger-ui/oauth2-redirect.html","url":"/openapi.yaml","validatorUrl":""}
The problem happened only in my machine, other colleagues' machines work fine with the same codebase.
Deps:
org.springdoc:springdoc-openapi-ui:1.6.11
org.springdoc:springdoc-openapi-javadoc:1.6.11
org.springdoc:springdoc-openapi-data-rest:1.6.11
org.springdoc:springdoc-openapi-security:1.6.11
io.swagger:swagger-annotations:1.6.6
org.openapitools:jackson-databind-nullable:0.2.2
io.swagger.parser.v3:swagger-parser:2.0.32
org.openapi.generator version "5.4.0" // open API generator plugin

FastAPI - Unable to render Swagger in production

This is my FastAPI main.py file.
from fastapi import FastAPI
from project.config.settings import base as settings
app = FastAPI(docs_url=f"{settings.URL_ROOT}/{settings.DOCS_URL}", redoc_url=None)
app.openapi_version = "3.0.0"
# some functions here
And I deployed this project to a server. But when I go to address of docs in my server, 1.2.3.4/url_root/docs_url, it shows me following message:
Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field.
Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).
What's the problem and how can I solve it?
UPDATE:
FastAPI is behind Nginx. All of my endpoints are working correctly, but I cannot see docs.
You should check this page for proxy settings.
but as far as i understand, you can fix this by just adding root_path to openapi_url:
app = FastAPI(
docs_url=f"/url_root/docs_url",
openapi_url="/url_root/openapi.json",
redoc_url=None)

Django rest framework swagger missing port when run in docker

I am using rest framework swagger for the first time in my Django application. When I run it locally from PyCharm it works just fine. My app runs on port 1337 and when I Try Out my restful API endpoint and click Execute, the curl command works and the URL includes the port.
The issue is when I run my Django app in a Docker. In this case, the URL in the curl command does not include the port. Do I have to add any swagger specific configuration to my docker compose file? I have not changed my Dockerfile nor my docker-compose file at all.
What do I need to do to get this to work properly?
It seems like you have set the url parameter for the get_schema_view(...)--(DRF Doc) function. The url is used to set the canonical base URL for the schema.
If you didn't set the value, DRF will use the "requested host" as the default URL.
Ref
url = self.url
if not url and request is not None:
url = request.build_absolute_uri()
SO, you can set the url to any value or you can exclude the parameter to get the default behavior.
from rest_framework.schemas import get_schema_view
urlpatterns = [
path('openapi', get_schema_view(
title="Your Project",
description="API for all things …",
version="1.0.0"
), name='openapi-schema'),
]
Please check the file urls.py which is the place you are setting URL for SWAGGER.
Don't set url in get_schema_view function.
Also, when you are running the app by using docker, the URL in the curl command does not include the port -> That's right!
I think that your file is using multiple settings for swagger by checking like this:
if getattr(settings, "SETTINGS_ENV", None) in ["local", "dev", "draft", "production"]:

Debug authentication of Bazel's http_file

I want to fetch some data in Bazel over HTTP. There's a http_file method that looks like what I want. The remote server I'm fetching from uses authentication, so I've written it as
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
http_file(
name = "data_file",
urls = ["https://example.com/data.0.1.2"],
sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
downloaded_file_path = "data_file",
)
When I try the build, I get
WARNING: Download from https://example.com/data.0.1.2 failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 401 Unauthorized
Followed by fatal errors because the file doesn't exist.
The error makes me think that I'm not authenticating correctly. I have a .netrc file and curl is able to use it to fetch the file.
Is there a way for me to debug? If it was curl, I would pass -v and see the auth header being sent with the request. I'm not sure if bazel is failing to send any authentication or if it's incorrect.
Running Bazel 3.2.0 on Mac OS (Darwin 19.6.0) and on Linux (Ubuntu 18.04).
HTTP 401 indeed sounds like incorrectly or not at all authenticated. .netrc should be supported and recognized. If not explicitly specified with netrc attribute, ${HOME}/.netrc would be tried if HOME is in the environment and bazel runs on non-Windows host (this has been the case since bazel 1.1; and shortly in 0.29) or %USERPROFILE%/.netrc if the variable is in the environment and running on Windows (this has been the case since 3.1). At the risk of stating the obvious, the .netrc should be owned by the same UID under which the process using it runs and its permbits should be 0600. If authentication methods other then http basic are needed, auth_patterns attribute needs to be used to configure that.
I am not aware of there being any ready made repository rule debugging facility such as CLI flag, but in this case it should be viable to copy the implementation of of the rule and functions it uses from tools/build_defs/repo, instrument it to get debugging info from it and use that for the purpose. For starters perhaps just print(auth) of what auth = _get_auth(ctx, all_urls) yielded to see if the that rule got the right idea about how to talk to host in question. It should be a dict with type, login, password information for each individual urls entries. The magic itself happens in use_netrc.

How to install django cors on server host computer

I developed a webapp using Django framework and hosted it on Digital Ocean. The server is supposed to receive a Json from a client written using Ruby rails framework.
The Django app cannot see the Json and so I want use django-cors-headers https://github.com/ottoyiu/django-cors-headers which require me to install django-cors-headers, but I have already hosted the app (i.e. the server).
My questions are ( sorry I am newbie)
Is it possible to install django-cors-headers using the pip install django-cors-headers on the server host computer at Digital Ocean using may be PUTTY or Winscp? If yes, pls how do i do it?
Did I choose the right solution to my problem?
Below is the code for the server (views.py) and I am using Django 1.9 and python 2.7.6
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
import json
def qucserver (request):
if request.method == 'POST':
print request.body
jsdata = json.loads(request.body)
reply = {"data": jsdata}
return JsonResponse ( reply )
else:
message = 'You submitted an empty form.'
reply = {"Error": message}
return JsonResponse (reply)
The output is always
{data: "json = JSON_DATA"}
If you have shell access you certainly ought to be able to install the django-cors-headers app or any other app using pip. However if your virtualenv is read only, you can simply copy the files from django-cors-headers into your project.

Resources