# Convert the file into base64 in elasticsearch for attachment

**URL:** https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971
**Category:** Elasticsearch
**Created:** [September 10, 2012, 7:53am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971 "2012-09-10T07:53:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://discuss.elastic.co/u/Suraj)
#### Post date: [September 10, 2012, 7:53am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/1 "2012-09-10T07:53:24Z")

</div>

Actually, I want to change the file into base64 and attach with my elastic  
search JSON data.  
My Code is given below:

```
curl -XDELETE "http://localhost:9200/test"

curl -XPUT "http://localhost:9200/test/?pretty=1" -d '
{
    "mapping" : {
        "xmlfile" : {
            "properties" : {
                "attachment": { "type" : "attachment" }
            }
        }
    }
}'

curl -XPOST "http://localhost:9200/test/xmlfile?pretty=1" -d '
{
    "attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'
}'

curl -XGET "http://localhost:9200/test/xmlfile/_search?pretty=1" -d '
{
    "query" : {
        "text" : {
            "file" : "any text will come here"
        }
    }
}'

```

When I execute this queries, specially while posting data I get this Error:

"error" : "MapperParsingException[Failed to parse]; nested:  
JsonParseException[Unexpected character ('P' (code 80)): expected a valid  
value (number, String, array, object, 'true', 'false' or 'null')\n at  
[Source: [B@4daa8b42; line: 3, column: 17]]; ", "status" : 400

Is there any solution for this kind of problem? I am trying to change the  
data into base64 while attaching the file.  
_Help??_

--

---

<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 10, 2012, 8:07am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/2 "2012-09-10T08:07:13Z")

</div>

I think that your concern is here:  
"attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'  
you use a simple ' to wrap your base64 content.  
You should have a "

So your JSON content is like :

> curl -XPOST "[http://localhost:9200/test/xmlfile?pretty=1](http://localhost:9200/test/xmlfile?pretty=1)" -d '  
> {  
> "attachment" : 'XXXXXX'  
> }'

which is IMHO, incorrect

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 10 sept. 2012 à 09:53, suraz [surajyonzon@gmail.com](mailto:surajyonzon@gmail.com) a écrit :

> Actually, I want to change the file into base64 and attach with my Elasticsearch JSON data.  
> My Code is given below:
> 
> ```
> curl -XDELETE "http://localhost:9200/test"
> 
> curl -XPUT "http://localhost:9200/test/?pretty=1" -d '
> {
> "mapping" : {
> "xmlfile" : {
> "properties" : {
> "attachment": { "type" : "attachment" }
> }
> }
> }
> }'
> 
> curl -XPOST "http://localhost:9200/test/xmlfile?pretty=1" -d '
> {
> "attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'
> }'
> 
> curl -XGET "http://localhost:9200/test/xmlfile/_search?pretty=1" -d '
> {
> "query" : {
> "text" : {
> "file" : "any text will come here"
> }
> }
> }'
> 
> ```
> 
> When I execute this queries, specially while posting data I get this Error:
> 
> "error" : "MapperParsingException[Failed to parse]; nested: JsonParseException[Unexpected character ('P' (code 80)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@4daa8b42; line: 3, column: 17]]; ", "status" : 400  
> Is there any solution for this kind of problem? I am trying to change the data into base64 while attaching the file.  
> Help??
> 
> --

--

---

<div class="post-metadata">

### Author: ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://discuss.elastic.co/u/Suraj)
#### Post date: [September 10, 2012, 8:55am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/3 "2012-09-10T08:55:52Z")

</div>

Actually, I want to convert the file into base64 while posting the data  
instead of converting the file into base64 format separately.

I think, there is a way for doing this but have to little dig up.

On Monday, September 10, 2012 1:52:22 PM UTC+5:45, David Pilato wrote:

> I think that your concern is here:
> 
> "attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'
> 
> you use a simple ' to wrap your base64 content.  
> You should have a "
> 
> So your JSON content is like :
> 
> curl -XPOST "[http://localhost:9200/test/xmlfile?pretty=1](http://localhost:9200/test/xmlfile?pretty=1)" -d '  
> {  
> "attachment" : 'XXXXXX'  
> }'
> 
> which is IMHO, incorrect
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 10 sept. 2012 à 09:53, suraz \<[suraj...@gmail.com](mailto:suraj...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Actually, I want to change the file into base64 and attach with my elastic  
> search JSON data.  
> My Code is given below:
> 
> ```
> curl -XDELETE "http://localhost:9200/test"
> 
> curl -XPUT "http://localhost:9200/test/?pretty=1" -d '
> {
> "mapping" : {
> "xmlfile" : {
> "properties" : {
> "attachment": { "type" : "attachment" }
> }
> }
> }
> }'
> 
> curl -XPOST "http://localhost:9200/test/xmlfile?pretty=1" -d '
> {
> "attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'
> }'
> 
> curl -XGET "http://localhost:9200/test/xmlfile/_search?pretty=1" -d '
> {
> "query" : {
> "text" : {
> "file" : "any text will come here"
> }
> }
> }'
> 
> ```
> 
> When I execute this queries, specially while posting data I get this Error:
> 
> "error" : "MapperParsingException[Failed to parse]; nested:  
> JsonParseException[Unexpected character ('P' (code 80)): expected a valid  
> value (number, String, array, object, 'true', 'false' or 'null')\n at  
> [Source: [B@4daa8b42; line: 3, column: 17]]; ", "status" : 400
> 
> Is there any solution for this kind of problem? I am trying to change the  
> data into base64 while attaching the file.  
> _Help??_
> 
> --

--

---

<div class="post-metadata">

### Author: ![gondo\_2](https://avatars.discourse-cdn.com/v4/letter/g/f08c70/32.png) [@gondo\_2](https://discuss.elastic.co/u/gondo_2)
#### Post date: [April 23, 2013, 8:00am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/4 "2013-04-23T08:00:46Z")

</div>

hi

what you have to do is add extra " quotes into the GET command:

curl -XPOST "[http://localhost:9200/test/xmlfile?pretty=1](http://localhost:9200/test/xmlfile?pretty=1)" -d '  
{  
"attachment" : "'`base64 /path/filename | perl -pe 's/\n/\\n/g'`'"  
}'

On Monday, 10 September 2012 17:53:24 UTC+10, suraz wrote:

> Actually, I want to change the file into base64 and attach with my elastic  
> search JSON data.  
> My Code is given below:
> 
> ```
> curl -XDELETE "http://localhost:9200/test"
> 
> curl -XPUT "http://localhost:9200/test/?pretty=1" -d '
> {
> "mapping" : {
> "xmlfile" : {
> "properties" : {
> "attachment": { "type" : "attachment" }
> }
> }
> }
> }'
> 
> curl -XPOST "http://localhost:9200/test/xmlfile?pretty=1" -d '
> {
> "attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'
> }'
> 
> curl -XGET "http://localhost:9200/test/xmlfile/_search?pretty=1" -d '
> {
> "query" : {
> "text" : {
> "file" : "any text will come here"
> }
> }
> }'
> 
> ```
> 
> When I execute this queries, specially while posting data I get this Error:
> 
> "error" : "MapperParsingException[Failed to parse]; nested:  
> JsonParseException[Unexpected character ('P' (code 80)): expected a valid  
> value (number, String, array, object, 'true', 'false' or 'null')\n at  
> [Source: [B@4daa8b42; line: 3, column: 17]]; ", "status" : 400
> 
> Is there any solution for this kind of problem? I am trying to change the  
> data into base64 while attaching the file.  
> _Help??_

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![gondo\_2](https://avatars.discourse-cdn.com/v4/letter/g/f08c70/32.png) [@gondo\_2](https://discuss.elastic.co/u/gondo_2)
#### Post date: [April 23, 2013, 8:01am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/5 "2013-04-23T08:01:15Z")

</div>

obviously POST not GET method

On Tuesday, 23 April 2013 18:00:46 UTC+10, gondo wrote:

> hi
> 
> what you have to do is add extra " quotes into the GET command:
> 
> curl -XPOST "[http://localhost:9200/test/xmlfile?pretty=1](http://localhost:9200/test/xmlfile?pretty=1)" -d '  
> {  
> "attachment" : "'`base64 /path/filename | perl -pe 's/\n/\\n/g'`'"  
> }'
> 
> On Monday, 10 September 2012 17:53:24 UTC+10, suraz wrote:
> 
> > Actually, I want to change the file into base64 and attach with my  
> > Elasticsearch JSON data.  
> > My Code is given below:
> > 
> > ```
> > curl -XDELETE "http://localhost:9200/test"
> > 
> > curl -XPUT "http://localhost:9200/test/?pretty=1" -d '
> > {
> > "mapping" : {
> > "xmlfile" : {
> > "properties" : {
> > "attachment": { "type" : "attachment" }
> > }
> > }
> > }
> > }'
> > 
> > curl -XPOST "http://localhost:9200/test/xmlfile?pretty=1" -d '
> > {
> > "attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'
> > }'
> > 
> > curl -XGET "http://localhost:9200/test/xmlfile/_search?pretty=1" -d '
> > {
> > "query" : {
> > "text" : {
> > "file" : "any text will come here"
> > }
> > }
> > }'
> > 
> > ```
> > 
> > When I execute this queries, specially while posting data I get this  
> > Error:
> > 
> > "error" : "MapperParsingException[Failed to parse]; nested:  
> > JsonParseException[Unexpected character ('P' (code 80)): expected a valid  
> > value (number, String, array, object, 'true', 'false' or 'null')\n at  
> > [Source: [B@4daa8b42; line: 3, column: 17]]; ", "status" : 400
> > 
> > Is there any solution for this kind of problem? I am trying to change  
> > the data into base64 while attaching the file.  
> > _Help??_

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![suriya](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@suriya](https://discuss.elastic.co/u/suriya)
#### Post date: [October 7, 2013, 8:57am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/6 "2013-10-07T08:57:20Z")

</div>

I indexed the attachment value into base64 but while searching it didn't get the result?  
Need to set anything?

---

<div class="post-metadata">

### Author: ![valerioorfano](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@valerioorfano](https://discuss.elastic.co/u/valerioorfano)
#### Post date: [August 17, 2016, 2:23pm UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/7 "2016-08-17T14:23:49Z")

</div>

Hi, how can i get the same result on windows platform? How can i encode the file base64 inside json string on the fly during document post ?

thanx a lot

valeiro

---

<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: [August 17, 2016, 3:19pm UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/8 "2016-08-17T15:19:00Z")

</div>

Don't answer to a thread which is 2 years old. You should open a new one and eventually link to this one.

You need to build a service which does that. Elasticsearch doesn't support dropping binary files directly. You could write a plugin may be.

---

<div class="post-metadata">

### Author: ![valerioorfano](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@valerioorfano](https://discuss.elastic.co/u/valerioorfano)
#### Post date: [August 18, 2016, 8:18am UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/9 "2016-08-18T08:18:48Z")

</div>

Thanx

actually i have noticed it is possible converting using a command line inside the \<\> field, for linux platform like :  
"attachment" : '`base64 /path/filename | perl -pe 's/\n/\\n/g'`'I am wondering how getting the same result in windows.

thanx valerio

---

<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:26pm UTC](https://discuss.elastic.co/t/convert-the-file-into-base64-in-elasticsearch-for-attachment/8971/10 "2017-07-05T22:26:59Z")

</div>


