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");
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
--