krakend

KrakenD: Trouble to upload file though gateway via POST request using Form-Data


Describe what are you trying to do

In one of my applications, I need to upload a file to my server from my angular website. Basically, to do this I use the FormData object to which append several informations, like the file name, and others. To send the file itself I will append to the FormData an fs.readStream(). Then I post this via axios to my server endpoint.

Exemple of code (postman request using form-data):

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();

data.append('avatar', fs.createReadStream('/home/file.mp3'));
data.append('title', 'test');
data.append('description', 'test');
var config = {
  method: 'post',
  url: 'localhost:8080/upload-file',
  headers: { 
    ...data.getHeaders()
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

Concerning the server, it is developed in node.js and I use the "multer" middleware to retrieve the file.

Exemple of an endpoint code :

import {Response, Request} from "express";
public static async UploadFile(req: Request, res: Response): Promise<any> { }

Without krakend gateway, it's works perfectly and I can then retrieve the file in my endpoint so that: req.file The others informations sent like "title", "description" are in req.body

Using krakend, I get all the information on the server side except the file, in the request, I only find the req.body and not the req.file

So my question is, how come krakend is not sending the file data to the backend and what would be the solution in order to send file via POST request a FormData to krakend ?

Your configuration file The content of your krakend.json:

{
  "version": 3,
  ...
  {
      "endpoint": "/upload",
      "method": "POST",
      "output_encoding": "no-op",
      "backend": [
        {
          "method": "POST",
          "encoding": "no-op", 
          "url_pattern": "/upload-file",
          "host": [
            "http://containername:8080"
          ]
        }
      ]
    }
}

I tried to use the different "no-op" annotations but nothing works, I have the impression that krakend does not interpret my file upload

Commands used How did you start the software?

I use docker-compose:

  krakend:
    container_name: 'Gateway'
    image: devopsfaith/krakend
    volumes:
      - ./KrakenD/dev/:/etc/krakend
    ports:
      - "8080:8080"
      - "1234:1234"
      - "8090:8090"
    links:
      - some containers
      - ...
    restart: always
    network_mode: bridge

Logs I don't have a specific log, only my backend which returns a 400 code as it can't find the file information in the request.


Solution

  • for those who are going through the same problem. Please note that postman was the cause of the problem for me, KrakenD does support sending bodies as multipart/form-data, so don't forget to let the headers through as needed.

    The problem is that when passing through postman, I can't explain but the file is badly sent to krakenD. Use Insomnia, or a simple Curl to do your tests.