by Kevin Schroeder | 7:48 am

I had a simple problem where I was trying to use the json array when uploading a file to Slack via the API. The code I was using was

$payload = [
             'json' => [
                 'channels' => $this->getChannel(),
                 'initial_comment' => $message,
                 'content' => $textAttachment,
                 'filetype' => 'text'
             ],
             'headers' => 'Authorization' => 'Bearer ' .  $this->getBearerToken()             
         ];
         $this->sendPayloadToSlack($payload, 
         'https://slack.com/api/files.upload');

But the problem was that I kept on getting a “new_file_data” error when I sent it.

stdClass Object
 (
     [ok] => 
     [error] => no_file_data
 )

It turns out that the problem was that I was not able to send the file upload via JSON data. So I changed Guzzle to use the multipart upload method as such:

$payload = [
    'multipart' => [
        ['name' => 'channels', 'contents' => $this->getChannel()],
        ['name' => 'initial_comment', 'contents' => $message],
        ['name' => 'content', 'contents' => $textAttachment],
        ['name' => 'filetype', 'contents' => 'text']
    ],
    'headers' => [
        'Authorization' => 'Bearer ' .  $this->getBearerToken()
    ]
];
$this->sendPayloadToSlack($payload, 'https://slack.com/api/files.upload');

And the result was:

stdClass Object
 (
     [ok] => 1
     [file] => stdClass Object
         (
<bunch of stuff>
         )
)

Comments

No comments yet...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.