PATCH request don't work in ODATA with DataServiceContext and If-Match

179 views Asked by At

The ODATA Version is 4 and the ODATA Client Version is 7.13.00.

I try to send a conditional PATCH request using If-Match header using the OData Client Context and a DataServiceCollection and SaveChanges() method as shown in the code below. In the If-Match header, the value is an old ETag (corresponding to MyProperty = 50), and I expect the PATCH request to throw an exception, as the entity has been updated, so it should fail, but instead, the update the of MyProperty property is done correctly.

  var retrievedEntity = clients.MyEntity.ByKey("1000").GetValue();
  retrievedEntity.MyProperty = 80;
  clients.UpdateObject(QueryBC6TransferOrder);
  DataServiceResponse response = clients.SaveChanges(SaveChangesOptions.None);

I tried another solution using the clients.Execute method as show below, but with no luck because it throws the exception: "The HttpMethod must be GET, POST or DELETE. (Parameter 'httpMethod')"

clients.Execute(clients.BC6_Transfer_OrderTransferLines.RequestUri, "PATCH", new BodyOperationParameter("QtyToReceive", -90) as OperationParameter);

Please note that we added the If-Match header for both solutions using the following code with uses the BuildingRequest event of the client where we specify the PATCH method to be used during the SaveChanges call:

private static void Context_BuildingRequest(object sender,Microsoft.OData.Client.BuildingRequestEventArgs e)
        {
            if (!e.Headers.ContainsKey("If-Match"))
            {
                e.Headers.Add("If-Match", "*");
            }
            else
            {
                e.Headers["Content-Type"] = "application/json";
                e.Method = "PATCH";
            }
        }

We assume either the request is not using the PATCH method or the If-Match header is not taken into account. Can someone help me to find a solution or understand what is wrong in the code. I followed the Microsoft documentation with no luck and the following tutorial:, but with POSTMAN, the If-Match header is taken into account and the entity is not updated if the ETag changed before the PATCH method. Thanks for any help or suggestions.

https://devblogs.microsoft.com/odata/tutorial-sample-client-property-tracking-for-patch/

Thank

0

There are 0 answers