Describe file content in swagger - swagger

I'm working on documenting our existing API's in swagger using open api 3.0.0 specification. I would like to understand the procedure on documenting the contents of a file that is to be uploaded. For e.g.
Say the file that is to be uploaded is response.json, and it has the following content:
{
name: xyz,
age: 50
}
I would like to know the procedure for documenting the above JSON so that clients using our API should provide the file in the aforesaid JSON format.

Related

Microsoft Graph (OneDrive) API - Resumable Upload Content-Type

I am trying to create the upload PUT request for the OneDrive API. It's the large file "resumable upload" version which requires the createUploadSession.
I have read the Microsoft docs here: As a warning the docs are VERY inaccurate and full of factual errors...
The docs simply say:
PUT
https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337Content-Length:
26Content-Range: bytes 0-25/128 <bytes 0-25 of the
file>
I am authenticated and have the upload session created, however when I pass the JSON body containing my binary file I receive this error:
{ "error": {
"code": "BadRequest",
"message": "Property file in payload has a value that does not match schema.", .....
Can anyone point me at the schema definition? Or explain how the JSON should be constructed?
As a side question, am I right in using "application/json" for this at all? What format should the request use?
Just to confirm, I am able to see the temp file created ready and waiting on OneDrive for the upload, so I know I'm close.
Thanks for any help!
If you're uploading the entire file in a single request then why do you use upload session when you can use the simple PUT request?
url = https://graph.microsoft.com/v1.0/{user_id}/items/{parent_folder_ref_id}:/{filename}:/content
and "Content-Type": "text/plain" header and in body simply put the file bytes.
If for some reason I don't understand you have to use single-chunk upload session then:
Create upload session (you didn't specified any problems here so i'm not elaborating)
Get uploadUrl from createUploadSession response and send PUT request with the following headers:
2.1 "Content-Length": str(file_size_in_bytes)
2.2 "Content-Range": "bytes 0-{file_size_in_bytes - 1}/{file_size_in_bytes}"
2.3 "Content-Type": "text/plain"
Pass the file bytes in body.
Note that in the PUT request the body is not json but simply bytes (as specified by the content-type header.
Also note that max chuck size is 4MB so if your file is larger than that, you will have to split into more than one chunks.
Goodlcuk

How to fix error 400 "file not found" when sending file to api endpoint on a REST POST snap on snaplogic?

I am trying to send a csv file from snaplogic to an api endpoint using through a REST POST snap but it doesn't seem to actually send the file as the response says file not found
when I do the post through postman it just fine and use the same information I use in the snap
my settings in the POST snap are as follows:
Label*= REST Post Service URL*=http://(ip:port)/v1/innovazones HTTP
entity=(blank) Batch size=(blank) Show all headers(selected) Single
file upload: File=…/shared/TEMPcsv.csv Single file upload: File
key=file Single file upload: Filename to be used=innovaZones_csv_file
Upload transfer request type=calculate content length Upload body
type=Multipart form data Single file upload: Multipart Content-Type=text/csv
HTTP header:
Key=Content-Type;Value=application/x-www-form-urlencoded
key=Authorization Value="Bearer " + account.access_token
Trust all certificates*(checked) Follow redirects*(checked)
I should be getting this message:
{
"status": "success",
"message": "Data transmitted successfully",
"payload": {}
}
but I get this in the response instead:
"error": "REST API service endpoint returned error result: status code = 400, reason phrase = Bad Request"
"error_entity": {
"status":"fail",
"message":"File not found",
"payload":{
"message":"File not found",
"statusCode":400,
"status":"fail"
}
}
"original": {filename:../shared/TEMPcsv.csv, result:overwritten, original:{, ...}}
"filename": "../shared/TEMPcsv.csv"
"result": "overwritten"
You have not added the actual files to upload. You have only set the name of the file.
As per the SnapLogic documentation -
Single file Upload: Filename to be used
This property can be configured if you are uploading a file, the value entered in this field will be treated as the name for the file being uploaded. You can retain the original file name by leaving this property blank.
What you also need to do is point to the actual file you want to upload in the Upload file(s) section.
Again, from SnapLogic documentation -
Upload File(s)
Lists out the file keys and file names of the files that you want to upload. By default, the Settings popup displays the fields required to upload a single file. Click the + button to add new rows to this fieldset. For each file that you want to upload, you need to specify the upload-file key, the location and name, the name of the file in the destination directory, and the multi-part content type associated with the file that you want to upload.
Please refer to - SnapLogic Docs - REST POST

File upload selector in Flasgger POST routes

In Flasgger, I'm trying to create documentation for route which accepts uploaded files. However, despite sticking to the specification, I cannot display file selector in Flasgger UI.
I'm using latest (as of today) flasgger==0.9.1 running OpenAPI 3 specs (as in "openapi": '3.0.0') and I saw this commit in Swagger-UI, that enables file selectors for POST file requests.
I know similar questions were asked before, but none of them related to OAS version 3.
My code snippet is below:
Upload a file
Returns ID of uploaded file
---
tags:
- name: "attachments"
schemes:
- "http"
consumes:
- application/octet-stream
produces:
- application/json
requestBody:
content:
application/octet-stream:
schema:
type: file
format: binary
responses:
200:
... etc
And I get just an empty block input in Flasgger UI. Is it possible Flasgger does not support it even though Swagger-UI does (I thought it's built on top of it)? Or the syntax is wrong?
You are mixing OpenAPI 2.0 and 3.0 syntax. In OAS3, files are described as binary strings, that is type: string and not type: file. Also, the consumes, produces and schemes keywords are not used in OAS3.
Try the following:
Upload a file
Returns ID of uploaded file
---
tags:
- attachments
requestBody:
content:
application/octet-stream:
schema:
type: string # <-------
format: binary
responses:
'200':
description: OK
content:
application/json:
schema:
# ... etc
Note that OAS3 file upload requires Swager UI 3.16.0+ but Flassger 0.9.1 is bundled with UI 3.14.2 and there seems to be no way to update the bundled Swagger UI. This possibility will be added in the next update, Flassger 0.9.2:
https://github.com/rochacbruno/flasgger#externally-loading-swagger-ui-and-jquery-jscss
So you'll need to wait for Flassger 0.9.2.
I also recommend that you check the generated OpenAPI file for syntax errors using the Swagger Editor, because syntax errors might cause incorrect parsing/rendering. This answer explains how to export your OpenAPI file from Swagger UI so that you can use it elsewhere.

Large (> 4mb) File Attachments

Using the Microsoft Graph API, it is possible to add file attachments to messages as described here. However, since REST requests have a total size limit of 4mb this does not allow for very large attachments.
A resumable upload session allows for larger uploads that may be referenced by reference attachments providing a download link. However, these links are obviously short-lived and we would like to clean up the files at some point.
Is there any way to create a message with persistent file attachments larger than 4mb? Thinking along the lines of a DriveItem->FileAttachment conversion here, but could not find anything on the topic. Help is very much appreciated!
As of May 2017 you can use the referenceAttachment resource type. This allows you to attach a DriveItem from the OneDrive to a message:
POST https://graph.microsoft.com/beta/me/messages/AAMkAGE1M88AADUv0uFAAA=/attachments
Content-type: application/json
Content-length: 319
{
"#odata.type": "#microsoft.graph.referenceAttachment",
"name": "Personal pictures",
"sourceUrl": "https://contoso.com/personal/mario_contoso_net/Documents/Pics",
"providerType": "oneDriveConsumer",
"permission": "Edit",
"isFolder": "True"
}

Can read local JSON using rest assured

I am learning rest assured and trying to read my local javascript file. I am not sure if I can do that
This is what I am trying
Response resp = get("C:/dev/testrest.js");
assertEquals(200, resp.getStatusCode());
Here testrest.js is JSON
I am getting this error
java.lang.AssertionError: expected:<200> but was:<404>
REST Assured is for testing HTTP services (servers) and not for working with Javascript files. If you're looking for easily extracting values out of a JSON document you should have a look at JsonPath which is shipped with REST Assured (see getting started guide).

Resources