# Elasticsearch seems does not support multipart/form-data requests

**URL:** <https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174>\
**Category:** Elasticsearch\
**Created:** [December 26, 2012, 7:59am UTC](https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174 "2012-12-26T07:59:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asoqa](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@asoqa](https://discuss.elastic.co/u/asoqa)\
**Post date:** [December 26, 2012, 7:59am UTC](https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174/1 "2012-12-26T07:59:32Z")

</div>

the curl command:  
curl -v -POST localhost:9200/\_bulk --data-binary @requests

the php client code:  
$post = array(  
"file1"=\>"@d:\requests"  
);

$ch = curl\_init();  
curl\_setopt($ch, CURLOPT\_URL, "[http://localhost:9200/\_bulk](http://localhost:9200/_bulk)");  
curl\_setopt($ch, CURLOPT\_POST, 1);  
curl\_setopt($ch, CURLOPT\_HEADER, 0);  
curl\_setopt($ch, CURLOPT\_POSTFIELDS,$post);  
curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, 1);  
curl\_setopt($ch, CURLOPT\_VERBOSE, 0);

$a=curl\_exec($ch);  
//$cinfo=curl\_getinfo($ch);  
print "result:" . $a;

curl\_close($ch);

this code returns an error message ""Failed to derive xcontent from  
org.elasticsearch.common.bytes.ChannelBufferBytesReference". Because the  
request header is something like this:  
DefaultHttpRequest(chunked: false)  
POST /\_bulk HTTP/1.1  
Host: localhost:9200  
Accept: _/_  
Expect: 100-continue  
Content-Type: multipart/form-data;  
boundary=----------------------------6a8a98c02c48  
Content-Length: 296

and the content is something like this:  
------------------------------7490d35780e0  
Content-Disposition: form-data; name="file1"; filename="d:\requests"  
Content-Type: application/octet-stream

{ "index" : { "\_index" : "test", "\_type" : "type1", "\_id" : "1" } }  
{ "field1" : "value1" }

------------------------------7490d35780e0--

While in elasticsearch code :  
public static XContentType xContentType(BytesReference bytes) {  
int length = bytes.length() \< GUESS\_HEADER\_LENGTH ? bytes.length()  
: GUESS\_HEADER\_LENGTH;  
if (length \> 2 && bytes.get(0) == SmileConstants.HEADER\_BYTE\_1 &&  
bytes.get(1) == SmileConstants.HEADER\_BYTE\_2 && bytes.get(2) ==  
SmileConstants.HEADER\_BYTE\_3) {  
return XContentType.SMILE;  
}  
for (int i = 0; i \< length; i++) {  
if (bytes.get(i) == '{') {  
return XContentType.JSON;  
}  
}  
return null;  
}

```
  The code can not find the character '{' in the first 20 characters, 

```

so it returns an error.

```
  If I use the curl command with --data-binary @request, or change the 

```

code "$post = array("file1"=\>"@d:\requests");" to $post =  
file\_get\_contents("d:\requests"), es can handle the case. At this time,  
the request header is :  
DefaultHttpRequest(chunked: false)  
POST /\_bulk HTTP/1.1  
Host: localhost:9200  
Accept: _/_  
Content-Length: 92  
Content-Type: application/x-www-form-urlencoded

--

---

<div class="post-metadata">

**Author:** ![asoqa](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@asoqa](https://discuss.elastic.co/u/asoqa)\
**Post date:** [December 27, 2012, 8:31am UTC](https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174/2 "2012-12-27T08:31:27Z")

</div>

> anyone give me a help?

--

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 27, 2012, 10:42am UTC](https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174/3 "2012-12-27T10:42:47Z")

</div>

Hey there,

if you post in curl with --data-binary your input data is used as is and  
put in the POST request.

Your first request however is different, as you are doing a multipart form  
data request. This does not send raw text data in the body, but rather does  
another encoding on top of it (namely MIME). This is not supported by  
elasticsearch - as it is not useful to do another encoding on top as needed  
in HTTP for sending files.

Hope this helps,

--Alexander

On Thu, Dec 27, 2012 at 9:31 AM, asoqa [asoqa51@gmail.com](mailto:asoqa51@gmail.com) wrote:

> anyone give me a help?
> 
> > 
> 
> --

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [December 27, 2012, 1:41pm UTC](https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174/4 "2012-12-27T13:41:16Z")

</div>

Elasticsearch does not support HTML, therefore it does not support  
multipart/form-data media type.

Jörg

--

---

<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, 2:58am UTC](https://discuss.elastic.co/t/elasticsearch-seems-does-not-support-multipart-form-data-requests/10174/5 "2017-07-06T02:58:24Z")

</div>


