Jump to content
tulio

TSslHttpRest with multipart/form-data

Recommended Posts

How to send files to an API using TSslHttpRest? In Postman I can, this is the http code:

POST /api/br-invoice-integration/v1/batches HTTP/1.1
Host: qed.onvio.com.br
Accept: application/json
Authorization: Bearer 42dc0b99-2d17-4898-9c85-0b52ae364c63Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file[]"; filename="/C:/MyFile.xml"
Content-Type: text/xml

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="query"

{"boxe/File":true}
----WebKitFormBoundary7MA4YWxkTrZu0gW

With SslHttpRest, I'm trying to SendStream, like that (similar to the sample OverbyteIcsHttpPost1):


  FileName := 'C:\MyFile.xml';

  RandBoundary := '-----------------------------' +
    IntToHex(Random(MaxInt), 8) + IntToHex(Random(MaxInt), 8);

  SslHttpRest1.ContentTypePost := 'multipart/form-data; boundary=' + RandBoundary;

  Header := RandBoundary + #13#10 +
    'Content-Disposition: form-data; name="file[]"; filename="' +
    'MyFile.xml' + '"' + #13#10 +
    'Content-Type: text/xml' +
    #13#10 + #13#10;

  Footer := RandBoundary + #13#10 +
    'Content-Disposition: form-data; name="query"' + #13#10 + #13#10 +
    '{"boxe/File":true}' + #13#10 + RandBoundary + #13#10;


  SslHttpRest1.SendStream := TMultiPartFileReader.Create(Filename, Header, Footer);
  SslHttpRest1.SendStream.Position := 0;

  SslHttpRest1.RestRequest(httpPOST, Myurl);

But it doesn't work, just get error:

Request completed: 500 
Request done, StatusCode #500
{"error":{"code":500,"errors":[],"message":"HTTP 400 Bad Request"}}

Has anyone ever needed to do something similar, would this be the way? If anyone can help, I appreciate it.

Share this post


Link to post

While ICS has MIME decoding units,  the only multipart/form-data encoding is in the OverbyteIcsHttpPost1 sample you've found. 

 

I think you need to set DebugLevel to DebugBody and makes sure your POST content is logged to see what the component is actually sending to the server.

 

Angus

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×