# Error putting base64 converted string into Elasticsearch

**URL:** https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595
**Category:** Elasticsearch
**Created:** [September 27, 2016, 10:41am UTC](https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595 "2016-09-27T10:41:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![avion](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@avion](https://discuss.elastic.co/u/avion)
#### Post date: [September 27, 2016, 10:41am UTC](https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595/1 "2016-09-27T10:41:13Z")

</div>

I create a simple mapping:

```
    curl -XPUT 'localhost:9200/ficherosindex?pretty=true' -d '{
      "mappings": {
        "items": {
           "dynamic": "strict",
           "properties" : {
                "title" : { "type": "string" },
                "body" : { "type": "string" },
                "attachments" : { "type": "attachment" }
    }}}}'

```

I make `PUT` the `title` and the `body`, leaving `attachments` empty.

```
    curl -XPUT 'localhost:9200/ficherosindex/items/1' -d '{
      "title": "This is a test title",
      "body" : "This is the body of the java",
      "attachments" : ""
    }'

```

And then I make the following script to update the `attachments` fields with the content of the `MY_PDF.pdf` file, converting it to `base64`.

```
    #!/bin/sh
    coded=`cat MY_PDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
    curl -X POST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -d '{
        "doc" : {
                "attachments" : \"${coded}\"
    }}'

```

When I run the script I'm getting the following error:

```
    {
      "error" : {
        "root_cause" : [ {
          "type" : "json_parse_exception",
          "reason" : "Unexpected character ('\\' (code 92)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@6c8caddf; line: 3, column: 30]"
        } ],
        "type" : "json_parse_exception",
        "reason" : "Unexpected character ('\\' (code 92)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@6c8caddf; line: 3, column: 30]"
      },
      "status" : 500
    }

```

What I'm doing wrong? Maybe I've to change the following line?

```
    {
        "doc" : {
           "attachments" : \"${coded}\"
    }}'

```

I also tried [this](http://stackoverflow.com/a/12347395/742269) solution with no luck. I have to mantain the order I'm showing. First create the item without the `attachments` and then use the `_update` to append the content of the .PDF to it.

Thanks in advance

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [September 27, 2016, 10:54am UTC](https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595/2 "2016-09-27T10:54:08Z")

</div>

This?

```auto
curl -X POST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -d '{
        "doc" : {
                "attachments" : "${coded}"
    }}'

```

---

<div class="post-metadata">

### Author: ![avion](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@avion](https://discuss.elastic.co/u/avion)
#### Post date: [September 27, 2016, 2:09pm UTC](https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595/3 "2016-09-27T14:09:05Z")

</div>

Thanks @dadoonet

I finally solved it using the following script as the content of `coded` needs to be loaded from a file. Otherwise you get a `curl: Argument list too long` error.

```
#!/bin/sh
coded=`cat MY_PDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`

curl -XPOST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -H "Content-Type: application/json" -d @- <<CURL_DATA
{ "doc": { "attachments": "$coded" }}
CURL_DATA
```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 29, 2016, 12:00pm UTC](https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595/6 "2016-12-29T12:00:34Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Probably you should run something like:

```auto
var=$(base64 "$filename"| perl -pe 's/\n//g');
var1=$(curl -XPOST 'http://localhost:9200/reports-'$date'/document/reports?pipeline=attachment&pretty' -H "Content-Type: application/json" -d @- <<CURL_DATA
{ "data": "$var" }
CURL_DATA)

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:04pm UTC](https://discuss.elastic.co/t/error-putting-base64-converted-string-into-elasticsearch/61595/9 "2017-07-05T22:04:12Z")

</div>


