# Unable to store binary in Elasticsearch through python REST API

**URL:** <https://discuss.elastic.co/t/unable-to-store-binary-in-elasticsearch-through-python-rest-api/245160>\
**Category:** Elasticsearch\
**Created:** [August 16, 2020, 7:35pm UTC](https://discuss.elastic.co/t/unable-to-store-binary-in-elasticsearch-through-python-rest-api/245160 "2020-08-16T19:35:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dibyendu\_Dutta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dibyendu_dutta/32/73594_2.png) [@Dibyendu\_Dutta](https://discuss.elastic.co/u/Dibyendu_Dutta)\
**Post date:** [August 16, 2020, 7:35pm UTC](https://discuss.elastic.co/t/unable-to-store-binary-in-elasticsearch-through-python-rest-api/245160/1 "2020-08-16T19:35:10Z")

</div>

I have created a index like

```
> PUT new_index_blob
> {
> "mappings": {
> "properties": {
> "name": {
> "type": "text"
> },
> "blob": {
> "type": "binary",
> "store": true
> }
> }
> }
> }

```

When I am trying to store a Base64 encoded binary value from dev tool like below , it working fine

```
> PUT new_index_blob/_doc/1
> {
> "name": "Test",
> "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
> }

```

I am able to get/search the same from dev tools

`GET /new_index_blob/_source/1`

But when I am trying to store the same using python REST API its not working , not giving any error too , below code snippet

```
path = "C:\\Users\\Administrator\\Desktop\\POC_LOG_Collector\\"
name= "100.97.16.31_2020_Jul_17_09_14.zip" # 53 MB
zip_file_path = os.path.join(path, name)
elastic_url= "http://%s/%s/%s"%(
  "localhost:9200",
  "new_index_blob",
  "_doc/2"
)
try:
    data = open(zip_file_path, "rb").read()
    encoded = str(base64.b64encode(data))
    upload_data={}

    upload_data["name"]=name
    upload_data["blob"]=encoded
except Exception as e:
    print(str(e))

try:
    resp_elastic = requests.put(
            elastic_url,
            headers=headers,
            data=json.dumps(upload_data),
            verify=False
    )
    print("ES Entry completed for {}".format(name))
except Exception as e:
    print(e)

```

What is the mistake I am doing . Also even after inserting it from Dev Tools the Doc count is showing 0 , is it expected for **datatype**  **binary** with **store true**  
.

---

<div class="post-metadata">

**Author:** ![Dibyendu\_Dutta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dibyendu_dutta/32/73594_2.png) [@Dibyendu\_Dutta](https://discuss.elastic.co/u/Dibyendu_Dutta)\
**Post date:** [August 17, 2020, 4:26am UTC](https://discuss.elastic.co/t/unable-to-store-binary-in-elasticsearch-through-python-rest-api/245160/2 "2020-08-17T04:26:59Z")

</div>

I have fixed it now , it was byte string conversion issue. ES was expecting base64 encoded string and I was sending bytes .

---

<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:** [September 14, 2020, 4:27am UTC](https://discuss.elastic.co/t/unable-to-store-binary-in-elasticsearch-through-python-rest-api/245160/3 "2020-09-14T04:27:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
