1. Clint Maxwell
  2. PowerBuilder
  3. Tuesday, 13 March 2018 01:34 AM UTC

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). 

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Tuesday, 13 March 2018 07:24 AM UTC
  2. PowerBuilder
  3. # Permalink

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)

 

 

 

Comment
  1. Clint Maxwell
  2. Tuesday, 13 March 2018 19:00 PM UTC
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)

  1. Helpful
There are no comments made yet.
Alfredo Santibanez Accepted Answer Pending Moderation
  1. Tuesday, 6 October 2020 21:33 PM UTC
  2. PowerBuilder
  3. # 1

 

 

Hi Renee, I am trying to make a token request to azure using  that works in Postman, but cannot make it from PB.

 

I tried using loo_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.6.0")  and  HttpClient with the same result

 

The content from postman is:

      • "multipart/form-data; boundary=--------------------------809472225359017257967844"
      • 619
    •  
      • "client_credentials"
      • "xxxxxxxxxxxxxxxxxxxxx"
      • "xxxxxx"
      • "https://apixxx.azurewebsites.net/.default"

I tried filling the variable like this:

s_BOUNDARY = "$$$Boundary$$$"  
body="grant_type:~"client_credentials~""+char(10)+&
"client_id:~"xxxxxxxxxxxxxxxxx~""+char(10)+&
"client_secret:~"xxxxxxxxxxxxxxxxxxxxx""+char(10)+&
"scope:~"https://apiedwh.azurewebsites.net/.default~""


lblob_data = blob ("--"  + ls_BOUNDARY + "~r~n" + &
                "~r~n~r~n" + body + "~r~n", EncodingUTF8!)

lblob_data = lblob_data + blob ("--"  + ls_BOUNDARY + "~r~n" , EncodingUTF8!)

 

    lnv_HttpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=--" + ls_BOUNDARY)
    integer li_rc
    li_rc = lnv_HttpClient.SendRequest("POST", requestendpoint,lblob_data)

 

The result is 400, does not recognize the parameters in the multiform part

Can you give a hint on what to look for.

 

Best Regards

Alfredo

 

 

 

 

 

 

Comment
  1. René Ullrich
  2. Wednesday, 7 October 2020 06:29 AM UTC
Hi Alfredo,

I can't see why you are using a boundary in your body? Seems to be no such part in Postman request?

Try to simple repeat what Postman sends.

HTH,

René
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.