@@ -7,6 +7,7 @@ using MbedTLS
77using Dates
88using TimeZones
99using LibCURL
10+ using HTTP
1011
1112import Base: convert, show, summary, getproperty, setproperty!, iterate
1213import .. OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
@@ -227,12 +228,40 @@ function prep_args(ctx::Ctx)
227228 isempty (ctx. file) && (ctx. body === nothing ) && isempty (ctx. form) && ! (" Content-Length" in keys (ctx. header)) && (ctx. header[" Content-Length" ] = " 0" )
228229 headers = ctx. header
229230 body = nothing
231+
232+ header_pairs = [convert (HTTP. Header, p) for p in headers]
233+ content_type_set = HTTP. header (header_pairs, " Content-Type" , nothing )
234+ if ! isnothing (content_type_set)
235+ content_type_set = lowercase (content_type_set)
236+ end
237+
230238 if ! isempty (ctx. form)
231- headers[" Content-Type" ] = " application/x-www-form-urlencoded"
232- body = URIs. escapeuri (ctx. form)
239+ if ! isnothing (content_type_set) && content_type_set != = " multipart/form-data" && content_type_set != = " application/x-www-form-urlencoded"
240+ throw (OpenAPIException (" Content type already set to $content_type_set . To send form data, it must be multipart/form-data or application/x-www-form-urlencoded." ))
241+ end
242+ if isnothing (content_type_set)
243+ if ! isempty (ctx. file)
244+ headers[" Content-Type" ] = content_type_set = " multipart/form-data"
245+ else
246+ headers[" Content-Type" ] = content_type_set = " application/x-www-form-urlencoded"
247+ end
248+ end
249+ if content_type_set == " application/x-www-form-urlencoded"
250+ body = URIs. escapeuri (ctx. form)
251+ else
252+ # we shall process it along with file uploads where we send multipart/form-data
253+ end
233254 end
234255
235- if ! isempty (ctx. file)
256+ if ! isempty (ctx. file) || (content_type_set == " multipart/form-data" )
257+ if ! isnothing (content_type_set) && content_type_set != = " multipart/form-data"
258+ throw (OpenAPIException (" Content type already set to $content_type_set . To send file, it must be multipart/form-data." ))
259+ end
260+
261+ if isnothing (content_type_set)
262+ headers[" Content-Type" ] = content_type_set = " multipart/form-data"
263+ end
264+
236265 # use a separate downloader for file uploads
237266 # until we have something like https://github.com/JuliaLang/Downloads.jl/pull/148
238267 downloader = Downloads. Downloader ()
@@ -249,19 +278,25 @@ function prep_args(ctx::Ctx)
249278 LibCURL. curl_mime_filedata (part, _v)
250279 # TODO : make provision to call curl_mime_type in future?
251280 end
281+ for (_k,_v) in ctx. form
282+ # add multipart sections for form data as well
283+ part = LibCURL. curl_mime_addpart (mime)
284+ LibCURL. curl_mime_name (part, _k)
285+ LibCURL. curl_mime_data (part, _v, length (_v))
286+ end
252287 Downloads. Curl. setopt (easy, LibCURL. CURLOPT_MIMEPOST, mime)
253288 end
254289 kwargs[:downloader ] = downloader
255290 end
256291
257292 if ctx. body != = nothing
258293 (isempty (ctx. form) && isempty (ctx. file)) || throw (OpenAPIException (" Can not send both form-encoded data and a request body" ))
259- if is_json_mime (get (ctx . header, " Content-Type " , " application/json" ))
294+ if is_json_mime (something (content_type_set , " application/json" ))
260295 body = to_json (ctx. body)
261- elseif (" application/x-www-form-urlencoded" == ctx . header[ " Content-Type " ] ) && isa (ctx. body, Dict)
296+ elseif (" application/x-www-form-urlencoded" == content_type_set ) && isa (ctx. body, Dict)
262297 body = URIs. escapeuri (ctx. body)
263- elseif isa (ctx. body, APIModel) && isempty ( get (ctx . header, " Content-Type " , " " ) )
264- headers[" Content-Type" ] = " application/json"
298+ elseif isa (ctx. body, APIModel) && isnothing (content_type_set )
299+ headers[" Content-Type" ] = content_type_set = " application/json"
265300 body = to_json (ctx. body)
266301 else
267302 body = ctx. body
0 commit comments