Search code examples
phpmongodbrestheart

RESTHeart issus with If-Match


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 ...


Solution

  • 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