1

Since my RESTHeart is internal so I wrote a gateway with 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

"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

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

I not sure where is the problem ...

j113203
  • 11
  • 3
  • *Since my RESTHeart is internal so I wrote a gateway with php.", because no-one would write a public interface in PHP. :) – BanksySan Apr 10 '16 at 14:47

1 Answers1

0

Um...the code doesn't have any problem and i found the reason..

because the mongodb data need to create from 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

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

j113203
  • 11
  • 3
  • Exactly, the Etag metadata is created by RESTHeart, is nothing mongoldb is aware of. Besides, note that in RESTHeart 2.0 beta there is a new configurable ETag policy that you could use to relax some constraints: – mturatti Apr 11 '16 at 08:13