1. Michael Kench
  2. PowerBuilder
  3. Monday, 29 January 2018 05:37 AM UTC

Hi People,

I need help to upload a file from our PB system to a server using a HTTP form.  I am trying to use the MSXML object but am getting an error saying "Invalid or missing payload file".The address,tokens, files etc work using the postman app so must be something in my code. See code below.

ls_Method = "POST"
ls_Url    = "https://site.com.au/api/message"
loo_xmlhttp = CREATE oleobject
loo_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.6.0")
loo_xmlhttp.open (ls_Method, ls_Url, false)
loo_xmlhttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitshort")
loo_xmlhttp.setRequestHeader("Authorization", "Bearer " + ls_token)
ls_data = '------WebKitshort~r~nContent-Disposition: form-data; name="payloadFile"; filename="C:\\temp\\notmuchhere.txt"~r~nContent-Type: text/plain~r~n~r~n~r~n------WebKitshort~r~nContent-Disposition: form-data; name="clientUUID"~r~n~r~nTEST-0001~r~n------WebKitshort~r~nContent-Disposition: form-data; name="messageType"~r~n~r~nSubmit.002.00~r~n------WebKitshort--'
loo_xmlhttp.send(ls_data) 

Do I need to include the file as a blob in the data. If so, any ideas how.  Thanks Michael

Michael Kench Accepted Answer Pending Moderation
  1. Tuesday, 30 January 2018 02:21 AM UTC
  2. PowerBuilder
  3. # 1

I think I solved myself. I changed the content-type of the payloadfile to application/octet-stream and simply included the file contents in the data. Thankfully the data is text and is working fine. Probably would need to change the code a bit to output a binary file.

ls_Method = "POST"
ls_Url    = "https://site.com.au/api/message"
li_FileNum = FileOpen("C:\\TEMP\\test.xml", TextMode!)
FileReadEx(li_FileNum, ls_filedata)
FileClose ( li_FileNum )
loo_xmlhttp = CREATE oleobject
loo_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.6.0")
loo_xmlhttp.open (ls_Method, ls_Url, false)
loo_xmlhttp.setRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitshort")
loo_xmlhttp.setRequestHeader("Authorization", "Bearer " + ls_token)
ls_data = '------WebKitshort~r~nContent-Disposition: form-data; name="payloadFile"; filename="test.xml"~r~nContent-Type: application/octet-stream~r~n~r~n'
ls_data = ls_data + ls_filedata
ls_data = ls_data + '~r~n~r~n------WebKitshort~r~nContent-Disposition: form-data; name="clientUUID"~r~n~r~nTEST-0001~r~n------WebKitshort~r~nContent-Disposition: form-data; name="messageType"~r~n~r~nSubmit.002.00~r~n------WebKitshort--'
loo_xmlhttp.send(ls_data)

 

Comment
There are no comments made yet.
Michael Kench Accepted Answer Pending Moderation
  1. Monday, 29 January 2018 22:00 PM UTC
  2. PowerBuilder
  3. # 2

Thanks for the response Marco,

The service isn't just going to receive the file. Do you know how i would go about including the other form data with the blob.  EG clientUUID, mesasgeType

 

ls_data = '------WebKitshort~r~nContent-Disposition: form-data; name="payloadFile"; filename="C:\\temp\\notmuchhere.txt"~r~nContent-Type: text/plain~r~n~r~n~r~n------WebKitshort~r~nContent-Disposition: form-data; name="clientUUID"~r~n~r~nTEST-0001~r~n------WebKitshort~r~nContent-Disposition: form-data; name="messageType"~r~n~r~nSubmit.002.00~r~n------WebKitshort--'

Cheers Michael

Comment
There are no comments made yet.
Marco Meoni Accepted Answer Pending Moderation
  1. Monday, 29 January 2018 08:54 AM UTC
  2. PowerBuilder
  3. # 3

Hi Michael,
store the file into a blob variable and send the blob itself:


ll_bloblen = Len(lblb_myinputfile)
...

loo_xmlhttp.setRequestHeader("Content-Length", String(ll_bloblen))
...
loo_xmlhttp.send(lblb_myinputfile)

 

Cheers,

Marco

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