# How to add attachment to ir.attachment through json\_rpc

**URL:** <https://discuss.tryton.org/t/how-to-add-attachment-to-ir-attachment-through-json-rpc/3504>\
**Category:** Developer\
**Tags:** ir\
**Created:** [November 30, 2020, 12:49pm UTC](https://discuss.tryton.org/t/how-to-add-attachment-to-ir-attachment-through-json-rpc/3504 "2020-11-30T12:49:23Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)\
**Post date:** [July 5, 2021, 4:19pm UTC](https://discuss.tryton.org/t/how-to-add-attachment-to-ir-attachment-through-json-rpc/3504/9 "2021-07-05T16:19:59Z")

</div>

Sorry for long posting this thread. When posting using JSOn we need to user base64.  
When go back to python, we need to change back to byte so it can be read.  
Here I posted the solution. Hope it can help to others:

```auto
class Attachment(metaclass=PoolMeta):
    __name__ = "ir.attachment"

    @classmethod
    def __setup__ (cls):
        super(). __setup__ ()
        cls. __rpc__.update(
            {
                "rft_attachment": RPC(readonly=False),
            }
        )

    @classmethod
    def rft_attachment(cls, values, *args):
        if not values:
            return
        pool = Pool()
        Attachment = pool.get("ir.attachment")
        val = values[0]
        base64data = val["data"]
        filename = val["filename"]
        resource = val["resource"]
        imgdata = base64.b64decode(base64data)
        (attach,) = Attachment.create(
            [
                {
                    "resource": resource,
                    "name": filename,
                    "data": imgdata,
                }
            ]
        )
        return [{"value": True}]

```

---

_[View the full topic](https://discuss.tryton.org/t/how-to-add-attachment-to-ir-attachment-through-json-rpc/3504)._
