# Updating data in elasticsearch from urllib2 (Python)

**URL:** <https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869>\
**Category:** Elasticsearch\
**Created:** [April 8, 2014, 9:11am UTC](https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869 "2014-04-08T09:11:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josep\_Floriach\_Vento](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Josep\_Floriach\_Vento](https://discuss.elastic.co/u/Josep_Floriach_Vento)\
**Post date:** [April 8, 2014, 9:11am UTC](https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869/1 "2014-04-08T09:11:06Z")

</div>

Hi everyone,

I'm trying to do a simple update. With curl simply I put following command  
in console.

curl -XPOST  
[http://localhost:9200/peticiones-2014.04.08/logs/pJ\_T-Lt1QW6u3luxVrI\_ZQ/\_update](http://localhost:9200/peticiones-2014.04.08/logs/pJ_T-Lt1QW6u3luxVrI_ZQ/_update)  
-d '{"doc": { "Parametros" : "PruebaParametros"}}'{'doc': '{ Parametros :  
PruebaParametros}'}

And it works perfectly. The problem is that I want do exactly the same  
thing in a script, with urllib2 in Python. My code is:

```
    values = { "doc" : "{ Parametros : PruebaParametros }" }

```

postData = urllib.urlencode(values)  
req = urllib2.Request(query, postData)  
response = urllib2.urlopen(req)

Query is  
"[http://localhost:9200/peticiones-2014.04.08/logs/pJ\_T-Lt1QW6u3luxVrI\_ZQ/\_update](http://localhost:9200/peticiones-2014.04.08/logs/pJ_T-Lt1QW6u3luxVrI_ZQ/_update)".  
But I'm getting the following error: "urllib2.HTTPError: HTTP Error 500:  
Internal Server Error"

What am I doing wrong?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/00c788f1-ebac-4904-a12d-1db32fd47c85%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00c788f1-ebac-4904-a12d-1db32fd47c85%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Honza\_Kral](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honza_kral/32/876_2.png) [@Honza\_Kral](https://discuss.elastic.co/u/Honza_Kral)\
**Post date:** [April 8, 2014, 9:20am UTC](https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869/2 "2014-04-08T09:20:31Z")

</div>

Hello Josep,

you need to send the data as json, not as urlencoded, so:

values = { "doc" : "{ Parametros : PruebaParametros }" }  
postData = json.dumps(values)  
req = urllib2.Request(query, postData)  
response = urllib2.urlopen(req)

but unless you have very specific requirements I'd strongly suggest  
you use the official python client - [http://elasticsearch-py.rtfd.org](http://elasticsearch-py.rtfd.org)

Hope this helps,  
Honza

On Tue, Apr 8, 2014 at 2:11 AM, Josep Floriach Ventosinos  
[josep.floriach.ventosinos@gmail.com](mailto:josep.floriach.ventosinos@gmail.com) wrote:

> Hi everyone,
> 
> I'm trying to do a simple update. With curl simply I put following command  
> in console.
> 
> curl -XPOST  
> [http://localhost:9200/peticiones-2014.04.08/logs/pJ\_T-Lt1QW6u3luxVrI\_ZQ/\_update](http://localhost:9200/peticiones-2014.04.08/logs/pJ_T-Lt1QW6u3luxVrI_ZQ/_update)  
> -d '{"doc": { "Parametros" : "PruebaParametros"}}'{'doc': '{ Parametros :  
> PruebaParametros}'}
> 
> And it works perfectly. The problem is that I want do exactly the same thing  
> in a script, with urllib2 in Python. My code is:
> 
> ```
> values = { "doc" : "{ Parametros : PruebaParametros }" }
> 
> ```
> 
> postData = urllib.urlencode(values)  
> req = urllib2.Request(query, postData)  
> response = urllib2.urlopen(req)
> 
> Query is  
> "[http://localhost:9200/peticiones-2014.04.08/logs/pJ\_T-Lt1QW6u3luxVrI\_ZQ/\_update](http://localhost:9200/peticiones-2014.04.08/logs/pJ_T-Lt1QW6u3luxVrI_ZQ/_update)".  
> But I'm getting the following error: "urllib2.HTTPError: HTTP Error 500:  
> Internal Server Error"
> 
> What am I doing wrong?
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/00c788f1-ebac-4904-a12d-1db32fd47c85%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00c788f1-ebac-4904-a12d-1db32fd47c85%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CABfdDio4\_BSFi-Hf5AQ\_-iMhkLRQq-jzvqXX7iiPLVaZcp249A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CABfdDio4_BSFi-Hf5AQ_-iMhkLRQq-jzvqXX7iiPLVaZcp249A%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Josep\_Floriach\_Vento](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Josep\_Floriach\_Vento](https://discuss.elastic.co/u/Josep_Floriach_Vento)\
**Post date:** [April 8, 2014, 10:36am UTC](https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869/3 "2014-04-08T10:36:35Z")

</div>

That was so helpful! Thanks!

However, my index is not being updated... I'm gonna try Python  
elasticsearch client...

Thanks again!

On Tuesday, April 8, 2014 11:20:31 AM UTC+2, Honza Král wrote:

> Hello Josep,
> 
> you need to send the data as json, not as urlencoded, so:
> 
> values = { "doc" : "{ Parametros : PruebaParametros }" }  
> postData = json.dumps(values)  
> req = urllib2.Request(query, postData)  
> response = urllib2.urlopen(req)
> 
> but unless you have very specific requirements I'd strongly suggest  
> you use the official python client - [http://elasticsearch-py.rtfd.org](http://elasticsearch-py.rtfd.org)
> 
> Hope this helps,  
> Honza
> 
> On Tue, Apr 8, 2014 at 2:11 AM, Josep Floriach Ventosinos  
> \<[josep.floria...@gmail.com](mailto:josep.floria...@gmail.com) \<javascript:\>\> wrote:
> 
> > Hi everyone,
> > 
> > I'm trying to do a simple update. With curl simply I put following  
> > command  
> > in console.
> > 
> > curl -XPOST
> 
> [http://localhost:9200/peticiones-2014.04.08/logs/pJ\_T-Lt1QW6u3luxVrI\_ZQ/\_update](http://localhost:9200/peticiones-2014.04.08/logs/pJ_T-Lt1QW6u3luxVrI_ZQ/_update)
> 
> > -d '{"doc": { "Parametros" : "PruebaParametros"}}'{'doc': '{ Parametros  
> > :  
> > PruebaParametros}'}
> > 
> > And it works perfectly. The problem is that I want do exactly the same  
> > thing  
> > in a script, with urllib2 in Python. My code is:
> > 
> > ```
> > values = { "doc" : "{ Parametros : PruebaParametros }" } 
> > 
> > ```
> > 
> > postData = urllib.urlencode(values)  
> > req = urllib2.Request(query, postData)  
> > response = urllib2.urlopen(req)
> > 
> > Query is  
> > "  
> > [http://localhost:9200/peticiones-2014.04.08/logs/pJ\_T-Lt1QW6u3luxVrI\_ZQ/\_update](http://localhost:9200/peticiones-2014.04.08/logs/pJ_T-Lt1QW6u3luxVrI_ZQ/_update)".
> 
> > But I'm getting the following error: "urllib2.HTTPError: HTTP Error 500:  
> > Internal Server Error"
> > 
> > What am I doing wrong?
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit
> 
> [https://groups.google.com/d/msgid/elasticsearch/00c788f1-ebac-4904-a12d-1db32fd47c85%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00c788f1-ebac-4904-a12d-1db32fd47c85%40googlegroups.com).
> 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/090f094c-e7c8-4163-971a-cd3633a48714%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/090f094c-e7c8-4163-971a-cd3633a48714%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![redrubia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/redrubia/32/1554_2.png) [@redrubia](https://discuss.elastic.co/u/redrubia)\
**Post date:** [April 9, 2014, 2:15am UTC](https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869/4 "2014-04-09T02:15:15Z")

</div>

Your index may not be updating due to the way you are creating the doc.

values = { "doc" : "{ Parametros : PruebaParametros }" }

Should be...

values = { "doc" : { "Parametros" : "PruebaParametros" } }

-\> Notice the change in quotes and where they are being placed

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/de8e51ec-4447-417a-bf8e-375d0a2f31ae%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/de8e51ec-4447-417a-bf8e-375d0a2f31ae%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:37am UTC](https://discuss.elastic.co/t/updating-data-in-elasticsearch-from-urllib2-python/16869/5 "2017-07-06T01:37:28Z")

</div>


