# Send a piece of a big upload

`PATCH https://api.mailmasker.org/api/v1/drive/uploads/:id`

Add the next piece of the file to an open upload.

- Who can use it: Any signed-in account
- Area: Drive
- Answers 200 OK when it works
- Page: https://developer.mailmasker.org/drive-upload-piece

The body is the raw bytes of the piece, not JSON, and an `Upload-Offset` header says where in the file the piece starts, which must be exactly how much Drive already has. The answer's `offset` is where the next piece starts. When the last byte lands, `done` is true and `file_id` is the new file.

## Parameters

| Name | In | Required | What it is | Example |
|---|---|---|---|---|
| `id` | path | yes | The upload_id from Start a big upload. | `0b6f3c1e-8d2a-4f7b-9e5c-3a1d2b4c6e8f` |
| `chunk` | body | yes | The next piece of the file, as the whole raw request body (send it as application/octet-stream), with an Upload-Offset header giving the byte it starts at.  |  |

## Good to know

- A missing or malformed Upload-Offset header answers 400.
- A piece that doesn't start where Drive's copy ends answers 409 with the true offset, so carry on from there. That is how a resend after a dropped connection sorts itself out.
- A piece that would run past the size you declared answers 409 too, and so does one sent to a finished upload, whose answer includes its file_id.

## Examples

cURL:

```bash
curl -s -X PATCH 'https://api.mailmasker.org/api/v1/drive/uploads/0b6f3c1e-8d2a-4f7b-9e5c-3a1d2b4c6e8f' \
  -H "Authorization: Bearer $TOKEN" \
  -H 'Upload-Offset: 0' \
  -H 'Content-Type: application/octet-stream' \
  --data-binary @piece-1.bin
```

Perl:

```perl
use Mojo::Base -strict;
use Mojo::File qw(path);
use Mojo::UserAgent;

my $ua  = Mojo::UserAgent->new;
my $res = $ua->patch('https://api.mailmasker.org/api/v1/drive/uploads/0b6f3c1e-8d2a-4f7b-9e5c-3a1d2b4c6e8f',
    { Authorization => "Bearer $ENV{TOKEN}", 'Upload-Offset' => 0,
      'Content-Type' => 'application/octet-stream' },
    path('piece-1.bin')->slurp)->result;
die $res->code, ' ', ($res->json('/error') // $res->message), "\n" unless $res->is_success;
print $res->body, "\n";
```

homelab-cli:

```bash
homelab-cli drive upload ./backup.iso
```

## Response

200 OK:

```json
{ "offset": 25165824, "done": false }
```
