0

I am trying to upload an image from a flutter android app to server (running on flask-python). I have used swagger 2.0 to generate both flask server and dart client api code. I am getting error 'Key error: file' on my server side whenever I try to call the image upload api.

Setup:
Client: flutter client on android
Server: Flask Python
Both are setup using Swagger 2.0

I have tried calling the api from SOAP UI and it works perfectly and my image gets uploaded to the server. However, when I try to call the same image upload api from android app via flutter api, it gives me error on my server.

SOAP UI call parameters:
Method: POST
Endpoint: http://localhost:5000
Resource: /images
Post QueryString = true

Parameters:
inFile file:[filename that I have uploaded in the attachment section]

Swagger configuration:
post:
  description: Creates a new Image
  operationId: createNewImage
  consumes:
    - multipart/form-data
  parameters:
    - in: formData
      name: inFile
      type: file
      required: true
      description: Image file to upload.
    - in: formData
      name: imageDescription
      type: string
      required: false
      description: Description of image file
  responses:
    '200':
      description: 200 response
      schema:
        $ref: '#/definitions/ImageRespModel'

Flutter Api calling code (Not working):

var api = new ServerApi.ImagesApi();
File _image = <image object clicked from camera>
String _filename = path.basename(_image.path);
MultipartFile _imageFile = MultipartFile.fromBytes(
                    'inFile', _image.readAsBytesSync(),
                    filename: _filename);
api.createNewImage(_imageFile);

Expected Results: The android api call should be able to upload the code in the same way as I am able to do it from SOAP UI. I think I am missing something in the android api call setup.

Error on the python server:
type_func = TYPE_MAP[_type] # convert value to right type
KeyError: 'file'

Karan
  • 11
  • 2
  • https://stackoverflow.com/a/49645074/2863386 – Shyju M Jan 31 '19 at 06:39
  • @ShyjuM, thanks a lot for the reference link. It helped me find the root cause of the issue. The error was in the swagger-codegen for dart itself wherein it was setting up the MultipartRequest.fields with the key of 'inFile' instead of 'file'. I fixed the api code generated by swagger and now I am able to upload the file with swagger-codegen for dart itself. I tried it on localhost and its working fine. Will try it on my remote server too. Appreciate your direction and help. – Karan Feb 01 '19 at 01:30

0 Answers0