New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload from external site: Content-Type isn't multipart/form-data #1131

Open
mbecker opened this Issue Nov 10, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@mbecker

mbecker commented Nov 10, 2018

Hi @kataras,

thanks again for the nice server framework, Quite fast and easy to use; sometimes it takes a while to understand the philosopy but you have provided quite well documeneted examples. Thanks for that!

However I want to have Iris as a standalone backend service which handles only file upload. That means the clients like web frontend apps would send the data via xhr form upload. In the other issue I've noticed that the cors example may be not fully documented. But with your help the CORS is working as follows:

crs := func(ctx iris.Context) {
		ctx.Header("Access-Control-Allow-Origin", "*")
		ctx.Header("Access-Control-Allow-Credentials", "false")
		ctx.Header("Access-Control-Request-Headers", "X-File-Name,Cache-Control,Accept,content-type,X-Requested-With,Content-Length,Accept-Encoding,X-CSRF-Token,Authorization,token")
		ctx.Header("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,Content-Type")
		ctx.Next()
	}

v1 := app.Party("/api/v1", crs).AllowMethods(iris.MethodOptions) // <- important for the preflight.
	{
           // ....
        }

So now trying to upload files from an external page doesn't work as expected. The upload processing code for the backend server is from your example and is as follows:

v1.Post("/upload", iris.LimitRequestBodySize(10<<20), func(ctx iris.Context) {
			contentType := ctx.GetContentType()
			fmt.Printf("Content-Type: %s", contentType)

			// Get the file from the dropzone request
			uploadForm := UploadForm{}
			err := ctx.ReadForm(&uploadForm)
			if err != nil {
				ctx.StatusCode(iris.StatusInternalServerError)
				ctx.Application().Logger().Warnf("Error while parsing form: %v", err.Error())
			}

			fmt.Printf("JWT: %s\n", uploadForm.JWT)
			fmt.Printf("FORM VALUE: %s", ctx.FormValue("JWT"))
			file, info, err := ctx.FormFile("file")
			if err != nil {
				ctx.StatusCode(iris.StatusInternalServerError)
				ctx.Application().Logger().Warnf("Error while uploading: %v", err.Error())
				return
			}

			defer file.Close()
			fname := info.Filename

			// Create a file with the same name
			// assuming that you have a folder named 'uploads'
			out, err := os.OpenFile(filepath.Join(uploadDir, fname),
				os.O_WRONLY|os.O_CREATE, 0666)

			if err != nil {
				ctx.StatusCode(iris.StatusInternalServerError)
				ctx.Application().Logger().Warnf("Error while preparing the new file: %v", err.Error())
				return
			}
			defer out.Close()

			io.Copy(out, file)

			err = ch.Publish(
				s.AmqpExchange, // exchange
				"",             // routing key
				false,          // mandatory
				false,          // immediate
				amqp.Publishing{
					ContentType: "text/plain",
					Body:        []byte(fname),
				})
			failOnError(err, "Failed to publish a message")

			log.Printf(" [x] AMQP Sent to exchange '%s': %s", s.AmqpExchange, fname)
		})

For example I'm using your dropzone.js code and just modify the upload/endpoin url to the backend server. The error from the code above (backend upload server) is as follows:

[WARN] 2018/11/10 08:23 Error while uploading: request Content-Type isn't multipart/form-data

I've tried with different upload javascript tools like uppy (https://uppy.io/docs/xhr-upload/) and uppload (https://elninotech.github.io/uppload/). Doesn't work. The basic html form upload (fallback) works.

But all the javascript examples runs with nodejs as the backend upload server (expressjs + multer; no special configuration).

May I ask you where I'm starting to look at? Any idea what I could do?

Thanks for your help and keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment