Question
· Jul 2

getFile from rest api and swagger

I use a swagger file and ##class(%REST.API).CreateApplication to create the rest api.

There is an interesting post: https://community.intersystems.com/post/download-file-rest-api-operation, but it is code, not a swagger configuration. disp.cls returns always a header content : application/json that of course fails as I am not always returning a json

I can not figure out what to put in swagger. Some examples I tried:

produces:
- application/pdf
- image/png

responses:
   200:
      schema:
           type: file
responses:
   200:
       schema:
           type: string
           format: binary

Thank you

Discussion (2)1
Log in or sign up to continue

Hello @Sylvie Greverend

The sample swagger for produces "image/jpeg" and consumes "application/json" and "application/xml" for sample. Once the spec.cls. Once it's complied it creates a "DownloadImg" classmethod in .disp.cls

swagger

{
	"swagger": "2.0",
	"info": {
		"title": "test",
		"version": "1.0.0"
	},
	"paths": {
		"/Test": {
			"get": {
				"summary": "Test download",
				"operationId": "DownloadImg",
				"produces": [
					"image/jpeg"
				],
				"consumes": [
					"application/json",
					"application/xml"
				]
			}
		}
	}
}

Generated class method

ClassMethod DownloadImg() As %Status
{
    Try {
        Do ##class(%REST.Impl).%SetContentType("image/jpeg")
        If '##class(%REST.Impl).%CheckAccepts("image/jpeg") {
            Try {
                Do ##class(MyLearn.LearnREST.Swag.impl).%ReportRESTError(..#HTTP406NOTACCEPTABLE,$$$ERROR($$$RESTBadAccepts))
            } Catch {
                Do ##class(%REST.Impl).%ReportRESTError(..#HTTP406NOTACCEPTABLE,$$$ERROR($$$RESTBadAccepts))
            }
            Quit
        }
        Set response=##class(MyLearn.LearnREST.Swag.impl).DownloadImg()
        Do ##class(MyLearn.LearnREST.Swag.impl).%WriteResponse(response)
    } Catch (ex) {
        Try {
            Do ##class(MyLearn.LearnREST.Swag.impl).%ReportRESTError(..#HTTP500INTERNALSERVERERROR,ex.AsStatus(),$parameter("MyLearn.LearnREST.Swag.impl","ExposeServerExceptions"))
        } Catch {
            Do ##class(%REST.Impl).%ReportRESTError(..#HTTP500INTERNALSERVERERROR,ex.AsStatus(),$parameter("MyLearn.LearnREST.Swag.impl","ExposeServerExceptions"))
        }
    }
    Quit $$$OK
}

Some useful links 

HTH.