RocketChat / RocketChat/Rocket.Chat

Send Message with attachments API

Open
#24,456 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
46.1k
Forks
13.9k
Avg merge
3d 3h
Merged PRs (30d)
130

Description

Description:

When i send a message via REST API with attachments, it requires me to double array my the attachment for it to work.
documentation page: https://developer.rocket.chat/reference/api/rest-api/endpoints/team-collaboration-endpoints/chat-endpoints/sendmessage

Steps to reproduce:
  1. curl login? https://developer.rocket.chat/reference/api/rest-api/endpoints/other-important-endpoints/authentication-endpoints/login
  2. get ID and Auth token.
  3. Send message with attachment https://developer.rocket.chat/reference/api/rest-api/endpoints/team-collaboration-endpoints/chat-endpoints/sendmessage

this is the curl format i send (PHP):

`

$post = array(
    'message'=> array(
        'rid'=>'GENERAL',
        'msg'=>'test message',
        'attachments' => array (
        'image_url' => $picurl
        )
    )
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'X-Auth-Token: '.$token.'',
      'X-User-Id: '.$uid.'',
      'Content-type: application/json'
));`
Expected behavior:

sends the attached image + send message normally.
returns the success message

Actual behavior:

gives error:
Match error: Match error: Expected array, got {"image_url":"~~~.jpg"} in field attachments

Server Setup Information:
  • Version of Rocket.Chat Server: 4.4
  • Operating System: ec2 centos 7
  • Deployment Method: tar
  • Number of Running Instances: 1
  • DB Replicaset Oplog:
  • NodeJS Version: 14.18.2
  • MongoDB Version: 4.4.12
Client Setup Information
  • Desktop App or Browser Version:
  • Operating System:
Additional context

However, this is working when i changed the post_data format to something like this:

`

$post = array(
  'message'=>
    array(
      'rid'=>'GENERAL',
      'msg'=>'test message',
      'attachments' => array ( array(
        'image_url' => $picurl
      ))
    )
);`

see the attachments? i wrapped it with two(2) array(), and it worked.

I'm not sure if this is fixed already on 4.4.1, or it should really behave like this from the beginning. hope this helps other Rocket Chat API users out there. thank you RocketChat team.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Send Message REST API documentation and the PHP curl payload shown in the issue, then trace how the server validates the message.attachments field. Compare the documented and accepted attachment shapes. Done means the documented request format is consistent with validation and sending an image returns success without unnecessary array wrapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.