If-Match 的 REST Heart 问题
RESTHeart issus with If-Match
由于我的 RESTHeart 是内部的,所以我用 php 写了一个网关。
更新文档的代码是这样的..
function mongodb_PATCH($url,$data){
//my internal RESTHeart server
curl = curl_init("http://192.168.137.1:8080$url");
//create a PATCH request
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
//the custom header
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
"Content-type: application/json",
"If-Match: 570a01ca1bddd9b7f19ca799"
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
并将此函数称为
echo mongodb_PATCH("/oauth/user/*?filter={'id':'j113203'}",array (
"token" => "abcdef"
));
但 RESTHeart 响应错误代码 409
"http status code" : 409 ,
"http status description" : "Conflict" ,
"message" : "The document's ETag must be provided using the 'If-Match' header"
在mongodb中,数据存储为
{
"_id" : ObjectId("570a01ca1bddd9b7f19ca799"),
"id" : "j113203",
"pd" : "123456",
"token" : "abcd"
}
不知道哪里出了问题...
嗯...代码没有任何问题,我找到了原因..
因为mongodb数据需要从restheat
创建
function mongodb_PUT($url,$data){
$curl = curl_init("http://192.168.137.1:8080$url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
echo mongodb_PUT("/oauth/user/abc",array (
"pd" => "987654321",
"token" => "abc",
));
执行上面的代码会创建一个数据
{
"_id" : "abc",
"pd" : "987654321",
"token" : "123",
"_etag" : ObjectId("570a41186a48681068385634")
}
然后使用PATCH修改
function mongodb_PATCH($url,$data,$_etag){
$curl = curl_init("http://192.168.137.1:8080$url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'If-Match: '.$_etag
));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
echo mongodb_PATCH("/oauth/user/abc",array (
"token" => "123",
),"570a40956a48681068385633");
一切正常 :D
由于我的 RESTHeart 是内部的,所以我用 php 写了一个网关。
更新文档的代码是这样的..
function mongodb_PATCH($url,$data){
//my internal RESTHeart server
curl = curl_init("http://192.168.137.1:8080$url");
//create a PATCH request
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
//the custom header
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
"Content-type: application/json",
"If-Match: 570a01ca1bddd9b7f19ca799"
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
并将此函数称为
echo mongodb_PATCH("/oauth/user/*?filter={'id':'j113203'}",array (
"token" => "abcdef"
));
但 RESTHeart 响应错误代码 409
"http status code" : 409 ,
"http status description" : "Conflict" ,
"message" : "The document's ETag must be provided using the 'If-Match' header"
在mongodb中,数据存储为
{
"_id" : ObjectId("570a01ca1bddd9b7f19ca799"),
"id" : "j113203",
"pd" : "123456",
"token" : "abcd"
}
不知道哪里出了问题...
嗯...代码没有任何问题,我找到了原因..
因为mongodb数据需要从restheat
创建function mongodb_PUT($url,$data){
$curl = curl_init("http://192.168.137.1:8080$url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
echo mongodb_PUT("/oauth/user/abc",array (
"pd" => "987654321",
"token" => "abc",
));
执行上面的代码会创建一个数据
{
"_id" : "abc",
"pd" : "987654321",
"token" : "123",
"_etag" : ObjectId("570a41186a48681068385634")
}
然后使用PATCH修改
function mongodb_PATCH($url,$data,$_etag){
$curl = curl_init("http://192.168.137.1:8080$url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'If-Match: '.$_etag
));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
echo mongodb_PATCH("/oauth/user/abc",array (
"token" => "123",
),"570a40956a48681068385633");
一切正常 :D