Start a new topic
Answered

Write a binary file from an API REST

Hello,


Is there any possibility to directly generate a binary file returned during a call to an API REST ?

I think it's not possible directly with the API Rest component but with scripting. Could you please confirm or not ?


Thanks in advanced.


Best Answer

Hello,


Good news: this is directly possible with the native HTTP REST component, no scripting required.


When the API response contains binary data (a PDF, an image, an Excel file, etc.), you simply need to create a response content node using the BINARY media type instead of a text-based one:


1. Open your HTTP REST metadata, and go to the Operation's Response node.

2. Add (or edit) a content node under the Response, and set its Media Type to BINARY.


Once this BINARY node exists, it becomes available in your mapping as a regular field you can map to a target.


A very common pattern is to write that binary content directly to a file, by mapping this BINARY field to a File Server metadata's "binary_content" property field (this exact field name is required for a File metadata to write out the binary payload it receives). Just remember to also set the target file_path with the correct extension (for example, ending in ".pdf" if that's what the API returns).


Doc reference confirming this:

"To read binary data returned by REST APIs, use a content node with the BINARY media type."

https://www.semarchy.com/doc/semarchy-xdi/xdi/latest/Components/http-rest/howto/retrieve-full-response.html


Note: the "rawContent" field (used to capture the entire raw response, e.g. as JSON) does NOT support binary data - that one is text-only. For binary payloads specifically, you need the dedicated BINARY content node described above.


There's also a step-by-step example in our Knowledge Base showing exactly this scenario end-to-end (calling an API, retrieving a PDF from the response, and writing it to disk via a File Server metadata), if that helps:

https://www.semarchy.com/doc/semarchy-xdi/xdi/latest/Components/http-rest/howto/retrieve-full-response.html


So to directly answer your question: you do NOT need scripting for this. The BINARY media type response node handles it natively in the mapping.


Let us know if you'd like help setting this up on your specific HTTP REST metadata.


Best regards

1 Comment

Answer

Hello,


Good news: this is directly possible with the native HTTP REST component, no scripting required.


When the API response contains binary data (a PDF, an image, an Excel file, etc.), you simply need to create a response content node using the BINARY media type instead of a text-based one:


1. Open your HTTP REST metadata, and go to the Operation's Response node.

2. Add (or edit) a content node under the Response, and set its Media Type to BINARY.


Once this BINARY node exists, it becomes available in your mapping as a regular field you can map to a target.


A very common pattern is to write that binary content directly to a file, by mapping this BINARY field to a File Server metadata's "binary_content" property field (this exact field name is required for a File metadata to write out the binary payload it receives). Just remember to also set the target file_path with the correct extension (for example, ending in ".pdf" if that's what the API returns).


Doc reference confirming this:

"To read binary data returned by REST APIs, use a content node with the BINARY media type."

https://www.semarchy.com/doc/semarchy-xdi/xdi/latest/Components/http-rest/howto/retrieve-full-response.html


Note: the "rawContent" field (used to capture the entire raw response, e.g. as JSON) does NOT support binary data - that one is text-only. For binary payloads specifically, you need the dedicated BINARY content node described above.


There's also a step-by-step example in our Knowledge Base showing exactly this scenario end-to-end (calling an API, retrieving a PDF from the response, and writing it to disk via a File Server metadata), if that helps:

https://www.semarchy.com/doc/semarchy-xdi/xdi/latest/Components/http-rest/howto/retrieve-full-response.html


So to directly answer your question: you do NOT need scripting for this. The BINARY media type response node handles it natively in the mapping.


Let us know if you'd like help setting this up on your specific HTTP REST metadata.


Best regards

Login to post a comment