Sending pdf as multipart/form-data via pub.client.http method

I am using 10.3 and want to send a pdf file to a REST APİ via using pub.client.http method.

So anyone could help here ? I couldn’t find the way to add pdf file to a form_data body type.

Thanks in advance.

What does the “REST API” expose to support doing this?

Hi Mehmet,

I saw related post regarding to your problem,

Hopefully it helps, else we continue discuss in this post.

Thanks

Hi I also check that post and it couldn’t help my solution because he only describe how to send json format. I could not achieve to send pdf. I am getting 415 unsupported media type error from the server . Here is what I try ;

image


On server side, Developer told me that it needs to be multipart/form-data as content type, but I send application/pdf, but when I try to send multipart/form-data on http header content-type it gets 400 error directly.

And when I try to change contenttype on addbodypart method it gives an error like
“MIME part of type “multipart/form-data” contains object of type java.io.ByteArrayInputStream instead of MimeMultipart”

So I am glad to hear some help :slight_smile:

Now I changed the contentType in http header as multipart/form-data; boundary=--------------------------TEST
It gets
“{”$id":“1”,“message”:“Unexpected end of MIME multipart stream. MIME multipart message is not complete.”,“type”:“System.IO.IOException”,“date”:“2021-10-22T07:57:56.706142Z”}"

You’re on the right track. You create the multipart/form-data with the steps in your screen shot.

createMimeData -- set subType = form-data
You likely do not need to call addMimeHeader -- what are the inputs being set?
addBodyPart -- for the file; set these:
  content - the stream of the file content
  isEnvStream - no
  mimeHeader -
    Content-Disposition = form-data; name="upload" filename="%/yourVarWithFilenameNoPath%"
  contenttype - application/octet-stream; name="%/yourVarWithFilenameNoPath%" -- might specify application/pdf instead
  encoding - binary
  getEnvelopeStream -- set createMultipart = yes

http -- Nothing special needed here, just map the envStream to data/mimeStream; you could set Transfer-Encoding to chunked for efficiency

I see that the file is being read via FTP and is being loaded completely into memory. Are you sure you want to do that? You might consider a bit more modularity by:

  • Split the FTP steps to another service. Retrieve the file via FTP and store locally, temporarily.
  • Split the MIME stream creation to another service. Load the local file as a stream, not as bytes – don’t load the entire thing into memory. Pass that stream as the content to the addBodyPart call.

Splitting things up helps greatly with testing the individual major steps.

HTH.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.