Facing following error during https post. I want to send a Multipart/form-data to external application through https post but facing 400 Bad Request Error

Error: Failed to read the request form. Unexpected end of Stream, the content may have already been read by another component.

This is how im posting data . a json string and content in stream or bytes.
Verifying the content length looks like the stream im passing is not reaching or not being read.
Can someone help me.

@vankayala_madhavi Can you paste your complete Content-Type , What is defined in the boundary is not visible based on your screenshots ?

Refer : RFC1341(MIME) : 7 The Multipart content type

Sample :
Content-type: multipart/*; boundary=“simple boundary”
–simple boundary
Content-type: text/plain; charset=us-ascii

data/Content is not a valid input to pub.client:http.

data/string is used when you want to pass the string exactly as is – jsonString in isolation is likely not the content you want to be sending via multipart form. Indeed, I’m curious as to why JSON would be a value within a field of the form. Seems odd.

As @DINESH_J implies, there are a number of steps to follow to create a proper multipart form. Refer to the services in the MIME folder and the docs for how to use them to create and send a multipart form.

I used mime services.
Below is the Mime data formed.

Content-Type: multipart/mixed; boundary=“----=_Part_0_2028805265.1694708400029”

------=_Part_0_2028805265.1694708400029
Content-Type: text/plain;charset=UTF8
content-transfer-encoding: 8bit

Miscellaneous
------=_Part_0_2028805265.1694708400029
Content-Type: application/pdf;name=“Test.pdf”
content-transfer-encoding: 8bit

%PDF-1.7
%âãÏÓ
2 0 obj
<</Contents 3 0 R /MediaBox [0 0 612 792] /Parent 1 0 R /Resources 4 0 R /Rotate 0 /Tabs /A /Type /Page >>
endobj

4 0 obj
<</ProcSet [/PDF /ImageB /ImageC /ImageI] /XObject <</Im3 5 0 R >> >>
endobj

5 0 obj
<</BitsPerComponent 8 /ColorSpace /DeviceRGB /Filter /DCTDecode /Height 480 /Length 39384 /Subtype /Image /Type /XObject /Width 360 >>
stream
ÿØÿà

image

But still the attachment content is not going. Facing error like Content is required.
Is there any sample code for this i need to send pdf as an attachment stream or bytes.

It works from postman though.

Do not call streamToString. That is the issue Not only is it unnecessary, it is wrong – the stream does contain a string. It has multiple types of data, including the binary PDF. When you call streamToString, the PDF content is essentially made useless. Just pass the stream from getEnvelopeStream as data/mimeStream. Nothing else needs to be passed in the data doc.

When calling createMimeData, set the subType input to form-data. If you don’t do that, the Content-Type gets set to multipart/mixed, as shown in the MIME data you shared. You do not need to set any headers in the http call. When calling http with the mimeStream the headers from that are appended to the http headers (as described in the service documentation).

Why is there a jsonString being mapped to data/string? Based upon what you shared for the MIME steps there is no JSON content involved.

Follow the documentation. Don’t rely solely on trial-and-error to figure out what to do – which can be misleading as when you tried to pass “Content” as an input to http but that is not the way to do this.

1 Like

I would strongly encourage you to split the MIME data creation into its own service. That way you can run it, test it, tweak it, call it from another service to review what was created, etc. without posting it to the target.

And you can compare, to some degree, what you’re creating with your steps with the console output you see in Postman. So you can see what adjustments to make to match what Postman does behind the scenes.