98
Views
6
Comments
Solved
multipart file upload to OS binary file results in error when using XML extension
Application Type
Reactive
Service Studio Version
11.54.2 (Build 62176)

Hi, I'm new to the forum, I'll try my best to make my question as clear as possible.

Can someone please give me a hint on how to prevent the multipart part from getting into the text, or how best to remove unwanted text before using the XML with Xpath.


Description of application and details of problem

My application provides a rest api that sends an xml file to the rest api by multipart/formdata in te request. 


The rest api has an input parameter with type Binary data. After creating a new record with the binary data, a process is triggered to convert the binary file by the platforms XML extension from  BinaryToText and then proces the content with Xpath for further CRUD operations.

My problem is that the binary file after conversion to text contains the multipart headers content. So my binary to textFile contains this text. 



With the result that the XML cannot be read with Xpath, the action results in an error in this part of the action:

The error I am getting is: Data at the root level is invalid. Line 1, position 1.

I assume this is because the XML extension finds that the first lines of the XML are not valid XML.

Question to the forum

Can someone please give me a hint/advice on how to prevent the multipart part from getting into the text, or how best to remove unwanted text before using the XML with Xpath.



2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Marz,

I think you completely misunderstand how things work at a low level when exposing a REST API. When you expose a REST API that has a single parameter of type Binary Data, that is received in the body, you basically tell OutSystems that everything you are going to receive is structureless Binary Data. Everything sent as body in the request from an external source will be treated as one blob of data. It's totally up to your interpretation what that is going to be. There is absolutely no way¹ for OutSystems to know that there are two ways the data is going to be sent. In fact, it is breaking any sane convention of exposing/calling a REST API method, as you basically have the same method being called with totally different sets of data!

So your question "is there a way to send a multipart request that does not put the disposition data inside of the binary file" is totally misinformed. What the client sends you is a well-formed multipart/form-data request, which means there's a header, followed by binary data. In that header may be some indication you are dealing with a file. But since you expect a blob of binary data, everything the client sends is treated as structureless binary data. So the "disposition data" isn't "inside of the binary file", it is part of the payload that's sent to you, which includes both the header ("disposition data") and the file content.

Now, to resolve this mess you can do two things:

  1. Create two seperate methods, one for receiving a binary blob that you treat as an XML file, one for the multipart/form-data. The latter you can then process using the Multipart/form-data Forge asset.
  2. Accept that you may receive two very different types of data. Check the Content-Type header to see if it contains "multipart/form-data", and if so use the Multipart/form-data asset to process it, or do it "manually" like Matheus suggested.

I'd go for option 1, but it may be easier to use option 2.

¹Well, it could theoretically check the Content-Type header, assuming that the client sets it appropriately. But OutSystems doesn't have native handling of incoming multipart/form-data, so it wouldn't matter.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Marz,

It's unclear to me who is sending the multipart/form-data, and who is receiving it, and whether both are OutSystems REST APIs, or that one is an external API. Can you please tell me what the flow of data is? Who sends the XML file to whom?

2023-05-27 19-28-02
marz1966

Hey Killian tnkz for your time. 

In Outsystems I expose a RestApi with a input inputParameter Binary Data. I recieve the XML in the body. The XML is send by the client in 2 ways:

  1. The client sends from postman a binary file, 
  2. The client sends the XML as a multipart/form data

The first way, works fine. De binary file is saved to a record. And on create of that record, the XML is procecced by Outsystems trough use of the XML extensions of Outsystems, see third screenshot.

The second way does not work, The xml file contains also the disposition data from the headers, see please my second screenshot. And so processing  of the XML fails.

I want to know, is there a way to send a multipart request that does not put the disposition data inside of the binary file ?

I hope this give you more info

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Marz,

I think you completely misunderstand how things work at a low level when exposing a REST API. When you expose a REST API that has a single parameter of type Binary Data, that is received in the body, you basically tell OutSystems that everything you are going to receive is structureless Binary Data. Everything sent as body in the request from an external source will be treated as one blob of data. It's totally up to your interpretation what that is going to be. There is absolutely no way¹ for OutSystems to know that there are two ways the data is going to be sent. In fact, it is breaking any sane convention of exposing/calling a REST API method, as you basically have the same method being called with totally different sets of data!

So your question "is there a way to send a multipart request that does not put the disposition data inside of the binary file" is totally misinformed. What the client sends you is a well-formed multipart/form-data request, which means there's a header, followed by binary data. In that header may be some indication you are dealing with a file. But since you expect a blob of binary data, everything the client sends is treated as structureless binary data. So the "disposition data" isn't "inside of the binary file", it is part of the payload that's sent to you, which includes both the header ("disposition data") and the file content.

Now, to resolve this mess you can do two things:

  1. Create two seperate methods, one for receiving a binary blob that you treat as an XML file, one for the multipart/form-data. The latter you can then process using the Multipart/form-data Forge asset.
  2. Accept that you may receive two very different types of data. Check the Content-Type header to see if it contains "multipart/form-data", and if so use the Multipart/form-data asset to process it, or do it "manually" like Matheus suggested.

I'd go for option 1, but it may be easier to use option 2.

¹Well, it could theoretically check the Content-Type header, assuming that the client sets it appropriately. But OutSystems doesn't have native handling of incoming multipart/form-data, so it wouldn't matter.

2023-05-27 19-28-02
marz1966

Tnkz Killian for explaining this in detail. I will look into both suggested solutions.

Appreciate greatly your help with this

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Sure, np, glad I could be of help :).

2025-01-12 10-53-58
MatheusLima

What I will say its a work around, but after the conversion you can simply pass to the action the text starting in the tag initial of you xml "<xml?" using the "substr" and "index" functions. It will works, but probabily there is other best way to solved this rsrs.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.