Python code node - can it return binary?

I’ve read the documentation but I couldn’t find anything specifically about this problem. In my workflow I am reading a file (XLIFF v1.2) and converting it to XLIFF v2.1 in a Python code node. It is output as binary. The next node (HTTP request node) then tries to send the binary file to an API for translation. However, there is no meta data showing for the binary (e.g. no file size) and I get an error when I try to run the HTTP request node. Here are the 2 screenshots. The first screenshot shows the output from the Python code node where I am trying to convert my UTF-8 text into base64. You can see on the last few lines where I am converting from UTF-8 to base64.

The 2nd screenshot is the HTTP request node. It seems there is something missing because the binary inputs look like they are empty (there is no download button) and I do not know what to put into the fields in the HTTP request. Any ideas? I think the problem is in the code node and how it is returning the binary data. I don’t think it is returning the binary data correctly.

Python code node:

HTTP request node:

Hi,

Yes. You can return binary with both python and java script. There seems to be an issue with the code as it’s not returning the binary.

Could you share your workflow?

n8n attach workflow

Here is a quick example,

import csv
import io
import base64

csv_buffer = io.StringIO()
writer = csv.writer(csv_buffer)
writer.writerow(["Name", "Age"])
writer.writerow(["Alice", 30])
writer.writerow(["Bob", 25])

csv_content = csv_buffer.getvalue().encode("utf-8")  # convert to bytes
csv_buffer.close()

base64_data = base64.b64encode(csv_content).decode("utf-8")

return [{
    "binary": {
        "data": {
            "data": base64_data,
            "fileName": "people.csv",
            "mimeType": "text/csv"
        }
    }
}]

The next node (read from file)

Thank you @jabbson . I did eventually stumble on the solution before I read yours but it does work, My next node is an HTTP request node and I struggled to find a way to reference the inout binary. Eventually chatGPT gave me the solution which is to set the ‘name’ field to “file”. now I read all the documentation I could, and I never saw that listed anywhere. I’m posting this here in case others have the same issue. Here is a screenshot.