From 0fc02574bb686a9b49e5202ce08635a05d813e26 Mon Sep 17 00:00:00 2001 From: larry Date: Tue, 18 Jun 2019 09:04:53 +0800 Subject: [PATCH] fix multipart/form-data without filename and content type (#236) --- util.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/util.go b/util.go index ed9129ff..93498066 100644 --- a/util.go +++ b/util.go @@ -161,9 +161,19 @@ func escapeQuotes(s string) string { func createMultipartHeader(param, fileName, contentType string) textproto.MIMEHeader { hdr := make(textproto.MIMEHeader) - hdr.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, - escapeQuotes(param), escapeQuotes(fileName))) - hdr.Set("Content-Type", contentType) + + var contentDispositionValue string + if IsStringEmpty(fileName) { + contentDispositionValue = fmt.Sprintf(`form-data; name="%s"`, param) + } else { + contentDispositionValue = fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + param, escapeQuotes(fileName)) + } + hdr.Set("Content-Disposition", contentDispositionValue) + + if !IsStringEmpty(contentType) { + hdr.Set(hdrContentTypeKey, contentType) + } return hdr }