AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 7.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Antivirus

    • Introduction
    • How to install
    • Setup
    • How to use
    • Scanners

      • Introduction
      • ClamAV
      • Null
      • Custom
    • Events
    • PSR
  • Audit

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • Auth

    • Introduction
    • How to install
    • Fortify

      • Prerequisites
      • Actions

        • Rehash Password
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • How to install
    • Setup
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • ETags

    • Introduction
    • How to install
    • Setup
    • ETags usage

      • How to use
      • Generators

        • Default Generator
        • Custom Generator
      • Eloquent Models
    • Http Request Preconditions

      • Introduction
      • Resource Context
      • Preconditions
      • Actions
      • RFC 9110

        • If-Match
        • If-Unmodified-Since
        • If-None-Match
        • If-Modified-Since
        • If-Range
      • Extensions

        • Introduction
        • Range
      • Range Validator
      • Download Stream
    • Macros
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Caching
        • Registrar
      • Requests

        • Introduction
        • List Resources
        • List Deleted
        • Show Resource
        • Create Resource
        • Update Resource
        • Delete Resource
        • List Related
        • Process Multiple Resources
        • Helpers
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
        • Remove Response Payload
    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Sync
      • Flush
      • Hash
      • MIME-Type
      • Filename
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Translation

    • Introduction
    • How to install
    • Exporters

      • Introduction
      • Setup
      • How to use
      • Drivers

        • Introduction
        • Array
        • Lang.js (Array)
        • Lang.js (JSON)
        • Cache
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
      • Date Format
      • Semantic Version
You are viewing documentation for an outdated version. It is no longer supported!

Attachments

  • Add Attachment
  • Alternative Methods
    • Multiple Attachments
    • Attach Stream
    • Create Attachment
  • Remove Attachment

Add Attachment

You can use attachFile() to attach a file to your request. The method accepts four arguments:

  • $name: string Form input name
  • $path: string Path to file
  • $headers: array (optional) Http headers for attachment
  • $filename: string (optional) Filename to be used by request
$response = $client  
        ->attachFile('annual_report', '/reports/2023_annual.pdf')
        ->post('/reports/annual');

Alternative Methods

The withAttachment() method provides an alternative way of adding an attachment. It allows you to specify a callback, which is provided an Attachment instance. This is useful when you wish to specify a stream, rather than a path to a file. E.g. when you are dynamically creating the contents of an attachment.

use Aedart\Contracts\Http\Clients\Requests\Attachment;

$response = $client  
        ->withAttachment(function(Attachment $attachment){            
            $attachment
                ->name('annual_report')
                ->contents(fopen('data.csv', 'r'))
                ->filename('2023_annual.csv');
        })
        ->post('/reports/annual');

Multiple Attachments

Should you require to send multiple files using the above approach, then you may withAttachments(), which accepts an array of callbacks.

use Aedart\Contracts\Http\Clients\Requests\Attachment;

$response = $client
        ->withAttachments([
            function(Attachment $attachment){
                $attachment
                    ->name('finance_data')
                    ->contents(fopen('data.csv', 'r'))
                    ->filename('2020_finance.csv');            
            },
            function(Attachment $attachment){
                 // ...e.g. obtain data from database... not shown here...
                 $attachment
                    ->name('users_report')
                    ->contents($usersDataStream)
                    ->filename('users-report.txt');       
            },
            function(Attachment $attachment){
                $attachment
                    ->name('trending_chart')
                    ->attachFile('online_users_2020.png')
                    ->filename('online-users.png');
            },
        ])
        ->post('/reports/annual');

Attach Stream

The client also supports attaching a file stream, if needed. This can be done via the attachStream() method.

use Aedart\Streams\FileStream;

$response = $client  
        ->attachStream('2023_annual.pdf', FileStream::open('/reports/2023_annual.pdf', 'r'))
        ->post('/reports/annual');

Create Attachment

Using an array of callbacks, several attachments were added to a request, in the previous shown example. However, doing so can make your code slight bulky. This is especially true when you have many files you wish to attach. To split up the attachment logic, then you may find makeAttachment() useful, in that it creates an "empty" attachment instance. This allows you to create attachments in one part of your code, and add them to the request when needed.


$annualReportFile = $client->makeAttachment();
$annualReportFile
    ->name('annual_report')
    ->content(fopen('annual_report.pdf', 'r'));

$usersReportFile = $client->makeAttachment();
// ...etc

// ... later, when building your request
$response = $client
        ->withAttachments([
            $annualReportFile,
            $usersReportFile,
            $trendingChartFile,
        ])
        ->post('/reports/annual');

Tips

The withAttachment() method also accepts an Attachment instance directly.

$response = $client  
        ->withAttachment($annualReportFile)
        ->post('/reports/annual');

Remove Attachment

If you find yourself in a situation where you are required to remove an added attachment, then such can be done using withoutAttachment(). The method expects the "form input name" of the attachment as argument.

$builder = $client  
        ->withoutAttachment('annual_report');
Edit page
Last Updated: 18/03/2024, 10:45
Contributors: Alin Eugen Deac, alin
Prev
Payload
Next
Cookies