Solved

Trouble adding attachments to message/conversation Create API

  • 9 January 2023
  • 2 replies
  • 12 views

Badge +2

We are attempting to use the Front API Messages -> Import Message.

 

All of the documentation is shown with using JSON encoding with a note that requests using attachments must use form-data encoding.

 

We have the following request that is working when using JSON encoding.

curl --location --request POST 'https://api2.frontapp.com/inboxes/inb_INBOX/imported_messages' \

--header 'Accept: application/json' \

--header 'Authorization: Bearer TOKEN' \

--header 'Content-Type: text/plain' \

--data-raw '{

    "sender": {

        "name": "Tester",

        "handle": tester@example.com

    },

    "to": [],

    "body": "Test Message From Agent",

    "external_id": "12345",

    "created_at": 1654708989,

    "type": "sms",

    "metadata": {

        "thread_ref": "777",

        "is_inbound": false,

        "should_skip_rules": true

    },

    "attachments": []

}'

 

When executed, we get a 200 – OK message.

 

However, to get attachments to work, I have to adjust the call to use the form-data encoding. The documentation has a statement about form-data encoding:

 

File Upload

When you want to send files through the API, your request needs to be sent using the Content-Type HTTP header set to multipart/form-data.

The JSON data you would normally send in the request body needs to be split in multiple form fields (one for each property) named after the property key. If the property is an array, the name should include the index of the data (ie: {"to": [email1@example.com, email1@example.com]} would be split in two fields named to[0], to[1]). https://dev.frontapp.com/reference/introduction#file-upload

 

With the note above, we understand the syntax of arrays and how to convert those. However, the documentation fails to mention how to convert between nested objects. I would assume that we would use dot-delimitator or append underscores. Neither pattern appears to work.

curl --location --request POST 'https://api2.frontapp.com/inboxes/inb_INBOX/imported_messages' \

--header 'Accept: application/json' \

--header 'Authorization: Bearer TOKEN' \

--form 'sender.name="Tester"' \

--form 'sender.handle=tester@example.com' \

--form 'to[0]=recipient@example.com' \

--form 'body="Test Message from Postman"' \

--form 'external_id="12345"' \

--form 'created_at="1654708989"' \

--form 'type="sms"' \

--form 'metadata_thread_ref="777' \

--form 'metadata_is_inbound="false"' \

--form 'metadata_should_skip_rules="true"'

 

For sender, I have attempted the dot-notation and for metadata, I have attempted using underscores. Either method retures back the following error:

{

    "_error": {

        "status": 400,

        "title": "Bad request",

        "message": "Body did not satisfy requirements",

        "details": [

            "body.sender: missing",

            "body.metadata: missing"

        ]

    }

}

 

What is the correct syntax for sending over nested data structures?

icon

Best answer by Support Engineering 9 January 2023, 19:02

View original

2 replies

Badge +5

To send payloads with nested data structures and attachments, you'll need to set the Content-Type: multipart/form-data header, and send nested structures like so:

curl -X POST 'https://api2.frontapp.com/inboxes/inb_123/imported_messages' \

-H 'Authorization: Bearer API_TOKEN' \

-H 'Content-Type: multipart/form-data' \

--form 'external_id=8gfh9qh7qsadcqw7gfqfh7@mail.example.com' \

--form 'created_at=1619656054' \

--form 'sender[handle]=external.sender@example.com' \

--form 'sender[name]=External Sender' \

--form 'metadata[is_inbound]=true' \

--form 'metadata[is_archived]=false' \

--form 'to[]=front0@example.com' \

--form 'to[]=front1@example.com' \

--form 'body=MESSAGE_BODY' \

--form 'attachments[]=@/Users/me/Photos/cat.jpg' \

--form 'attachments[]=@/Users/me/Photos/dog.jpg'

 

I hope this is helpful - Please let me know if you have any further questions

 

--Jason

Badge +2

This is exactly what I was looking for.

Reply