Sending Attachments from an Email to an API

I am using N8N in a compose. I have an API, created in Python, to send to Analyze to Virus Total.

I want to download the attachments to send them to that API, but I am having a hard time attaching the files in the POST method.

This is the method to send the file, it works without problems.

@app.route(‘/analyze’, methods=[‘POST’])
def analyze_file():
# Check if the API Key is loaded
if not API_KEY:
return jsonify({“error”: “API Key not found”}), 400

# Check if a file is present in the request
if 'file' not in request.files:
    return jsonify({"error": "No file part in the request"}), 400

# Get the file from the request
file = request.files['file']

# Check if the file has a name
if file.filename == '':
    return jsonify({"error": "No selected file"}), 400

# If file exists, proceed to send it to VirusTotal for analysis
if file:
    # Save the file temporarily to send it to VirusTotal
    temp_path = Path(f"/tmp/{file.filename}")
    file.save(temp_path)

    # Open the file in binary mode and send it for analysis
    with open(temp_path, 'rb') as f:
        params = {'apikey': API_KEY}
        files = {'file': (file.filename, f)}
        response = requests.post(upload_url, files=files, params=params)

    # Check if the upload was successful
    if response.status_code == 200:
        result = response.json()
        scan_id = result['scan_id']

        # Wait for the file analysis to be processed
        time.sleep(10)

        # Query the report using the scan_id
        report_params = {'apikey': API_KEY, 'resource': scan_id}
        report_response = requests.get(report_url, params=report_params)

        # Check if the report retrieval was successful
        if report_response.status_code == 200:
            report_result = report_response.json()

            # Get the number of antivirus engines that flagged the file as malicious
            positives = report_result.get('positives', 0)
            total = report_result.get('total', 0)

            # Build the result object to be returned as JSON
            file_result = {
                "file_name": file.filename,
                "scan_id": scan_id,
                "positives": positives,
                "total": total,
                "is_malicious": positives > 0,
                "permalink": report_result.get('permalink')
            }

            # Return the result as JSON
            return jsonify(file_result), 200
        else:
            return jsonify({"error": "Error retrieving report from VirusTotal"}), 500
    else:
        return jsonify({"error": "Error uploading file to VirusTotal"}), 500

I’m getting errors like Bad request - please check your parameters

My HTTP Request configuration is to raise attachment_0. To do this configure Send Body with the following Form-Data parameters

I leave the image.

Any advice? Thank you very much!

  • n8n version: The lastest of DockerHub
  • Database (default: SQLite): Defautl
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: ARM on MAC

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

I solved it this way. But I don’t know how to do it if I have more than one attachment. How do I iterate?

I am not able to iterate over the attachments.

Today I have attachment_0 & attachment_1. How do I iterate? I tried with a function, but no luck. I’ll keep reading, but I know it’s a recurring problem, if anyone can help me I’d appreciate it.

Note: There can be 1, 2, X amount of attachments in each email.

Resolved!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.