简体   繁体   English

RESTHeart与If-Match一起发行

[英]RESTHeart issus with If-Match

Since my RESTHeart is internal so I wrote a gateway with php. 由于我的RESTHeart是内部的所以我用php编写了一个网关。

The code for updateing the document are like this.. 更新文档的代码是这样的..

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;
}

and call this function as 并将此函数称为

echo mongodb_PATCH("/oauth/user/*?filter={'id':'j113203'}",array (
    "token" => "abcdef"
));

But the RESTHeart response the error code 409 但RESTHeart响应错误代码409

"http status code" : 409 ,
"http status description" : "Conflict" ,
"message" : "The document's ETag must be provided using the 'If-Match' header"

in mongodb , the data are store as 在mongodb中,数据存储为

{
    "_id" : ObjectId("570a01ca1bddd9b7f19ca799"),
    "id" : "j113203",
    "pd" : "123456",
    "token" : "abcd"
}

I not sure where is the problem ... 我不确定问题出在哪里......

Um...the code doesn't have any problem and i found the reason.. 嗯...代码没有任何问题,我找到了原因..

because the mongodb data need to create from restheat 因为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",
));

do the above code will create a data 上面的代码会创建一个数据

{
    "_id" : "abc",
    "pd" : "987654321",
    "token" : "123",
    "_etag" : ObjectId("570a41186a48681068385634")
}

and then using PATCH to modify 然后使用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");

and all working fine :D 一切正常:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM