4

I'm developing a backup utility and need to upload files into server. I've build a simple rest API in Go and it uses a multipart form for files uploading and updating. Golang API parses this form as an usual HTML form and file field has an own name. But Qt (client) creates multipart form with file as the body (without name). What should I change?

Creating a multipart form in Qt:

QHttpPart contentPart;
contentPart.setHeader(
            QNetworkRequest::ContentDispositionHeader,
            QString(
                    R"(form-data; name="%1"; path="%2"; created="%3"; modified="%4"; package_id="%5")").arg(
                    file->name,
                    file->path,
                    QString::number(file->created.toSecsSinceEpoch()),
                    QString::number(file->modified.toSecsSinceEpoch()),
                    QString::number(file->package->id)
            )
);
contentPart.setHeader(QNetworkRequest::ContentTypeHeader, QMimeDatabase().mimeTypeForData(file->content).name());
contentPart.setBody(file->content);
multiPart->append(contentPart);

Processing a multipart form in Go:

incomingFile, handler, err := r.FormFile("upload")
if err != nil {
    log.Panicln(err)
    return
}
defer incomingFile.Close()

content, err := ioutil.ReadAll(incomingFile)
if err != nil {
    log.Panicln(err)
    return
}
path := r.FormValue("path")
created, _ := strconv.ParseInt(r.FormValue("created"), 10, 0)
modified, _ := strconv.ParseInt(r.FormValue("modified"), 10, 0)
file := schemes.File{
    Name:     handler.Filename,
    Path:     path,
    Content:  content,
    Created:  time.Unix(created, 0),
    Modified: time.Unix(modified, 0),
}
  • By *"as an usual HTML form"*, do you mean a form whose enctype is `application/x-www-form-urlencoded`? This is extremely inefficient to use for binary files, see [here](https://stackoverflow.com/q/4007969). I would suggest changing your Go webservice to accept `multipart/form-data`. – Mike May 31 '19 at 10:39
  • @Mike, no, in Go it's actually `multipart/form-data`, but Go's API is simmilar to `application/x-www-form-urlencoded`, as you can see in the code snippet. Under 'simmilar' I mean that file is a named field, but in Qt it isn't. – Nick Mironov May 31 '19 at 11:23
  • If it is only a difference in the high level API, normal `QHttpMultiPart` should work. Sorry, I am not familiar with Go. I might be able to help if you can provide a curl command that you would like Qt to mimic. – Mike May 31 '19 at 11:38

0 Answers0