HTTPClient POST with Multipart Form

1
0
-1

Similar to a question asked earlier, I'm trying to call a REST service to send a file.  Need to know if its possible and what it would look like to set up HTTPClient to execute a multipart POST request containing 3 parts (2 strings and a blob). 

Question Tags: 

Answers

René Ullrich answered HTTPClient POST with Multipart Form

1
0
-1

I use MS XMLHTTP ActiveX but I guess it's almost the same with PB HTTPClient.

You have to build the request body and include all data:

// ls_xml this is my xml as part of the content

// lblob_file is a file as part of the content

// ls_mimetype is the mimetype of the file

ls_BOUNDARY = "$$$Boundary$$$"   // use or build a string that is not part of the other content!

// build the content

lblob_data = blob ("--"  + ls_BOUNDARY + "~r~n" + &
                 'Content-Disposition: form-data; name="meta"' + &
                 "~r~n~r~n" + ls_xml + "~r~n", EncodingUTF8!)

lblob_data = lblob_data + blob ("--"  + ls_BOUNDARY + "~r~n" + &
                 'Content-Disposition: form-data; name="file"; filename="' + as_filename + '"' + &
                 "~r~nContent-Type: " + ls_mimetype + "~r~n~r~n", EncodingUTF8!) + &
                 lblob_file + blob ("~r~n--"  + ls_BOUNDARY + "--~r~n", EncodingUTF8!)

you have to set the content type in the request header and also the boundary

// with MS XMLHTTP:

lole_XmlHttp.setRequestHeader ("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)

 

 

 

clint.maxwell_20137's picture

Thanks René!

With your example above I was able to put it together and get it working. Knowing that it it was just a blob of other blobs did the trick. An example using HTTPClient is below. 

 

// Read the file into a blob
ll_FileNum = FileOpen('TEST.PDF', StreamMode!)
FileReadEx(ll_FileNum, blb_file)
FileClose(ll_FileNum)
 
ls_FileName = 'TEST.PDF'
ls_MimeType = 'application/pdf'
 
// Create a boundary marker for the multipart blob
ls_BOUNDARY = "$$$Boundary$$$"
 
// Create component blobs
blb_boundary = blob('~r~n--' + ls_Boundary + '~r~n', EncodingUTF8!)
 
blb_terminus = blob('~r~n--' + ls_Boundary + '--~r~n', EncodingUTF8!)
 
blb_filename = blob('Content-Disposition: form-data; name="File-Name"' + '~r~n~r~n' + ls_FileName, EncodingUTF8!)
 
blb_mimetype = blob('Content-Disposition: form-data; name="Mime-Type"' + '~r~n~r~n' + ls_MimeType, EncodingUTF8!)
 
blb_file = blob( 'Content-Disposition: form-data; name="File"; filename="' + ls_FileName + '"' + '~r~n' + &
                        "Content-Type: " + ls_MimeType + "~r~n~r~n", EncodingUTF8!) + &
                        blb_file
 
// Concatenate blobs into a single multipart blob
blb_multipart = blb_boundary + &
                        blb_filename + &
                        blb_boundary + &
                        blb_mimetype + &
                        blb_boundary + &
                        blb_file + &
                        blb_terminus
 
 
// HTTPClient
inv_httpClient.clearrequestheaders()
inv_httpClient.setrequestheader('Access-Token', sle_access_token.text)
inv_httpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)
 
inv_httpClient.SendRequest('POST', ls_url, blb_multipart)
inv_httpClient.GetResponseBody(ls_json)