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 4.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Origin
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • 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
    • registerAsApplication()
    • destroy()
  • Core

    • Athenaeum Core Application
    • Prerequisite
    • How to install
    • Integration
    • Usage

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

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

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

    • 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
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Driver Options
        • Driver
      • Http Query Builder

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

      • Http Cookies
      • How to install
      • Usage
  • Properties

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

    • Service Registrar
    • How to install
    • How to use
  • 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
  • Utils

    • Introduction
    • How to install
    • Array
    • Json
    • Math
    • Method Helper
    • Populatable
    • Version
You are viewing documentation for an outdated version. It is no longer supported!

Attachments

  • Add Attachment
  • Alternative Methods
    • Multiple Attachments
    • 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/2020_annual.pdf')
        ->multipartFormat()
        ->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('2020_annual.csv');
        })
        ->multipartFormat()
        ->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');
            },
        ])
        ->multipartFormat()
        ->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,
        ])
        ->multipartFormat()
        ->post('/reports/annual');

Tips

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

$response = $client  
        ->withAttachment($annualReportFile)
        ->multipartFormat()
        ->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: 08/09/2020, 18:52
Contributors: Alin Eugen Deac
Prev
Payload
Next
Cookies