2

我开发了一个连接到 Microsoft Dynamics NAV 2017 OData Web 服务的 php 应用程序,我可以毫无问题地读取(GET)和创建(POST),但是对于删除我收到错误 405,微软说可以删除:

https://msdn.microsoft.com/es-es/library/dd355398(v=nav.90).aspx

https://msdn.microsoft.com/en-us/library/dn182582(v=nav.90).aspx

我检查了 Dynamics NAV 中具有正确属性 InsertAllowed、ModifyAllowed 或 DeleteAllowed 的页面设置为 Yes,并且我有权删除

尝试使用邮递员后收到相同的错误:

使用邮递员删除错误 Odata 调试

有人能帮我吗?谢谢

4

1 回答 1

2

Finally I found the solution!! , I write myself to help another who is with the same problem:

You only have to add the identifier on the request url, in my case the identifier of the customer table ('/Customer(No='.$identifier.')')

This is a sample code in PHP with guzzle and table customer of Dynamics NAV:

 $client = new GuzzleHttpClient();
 $uri=env('HTTP_URIBASE', '');
 $apiRequest = $client->request('DELETE', $uri.'/Customer(No='.$identifier.')',[
        'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ],
        'headers' => ['Content-Type' => 'application/json', 
                       'Accept' => 'application/json']
  ]);
  $content = json_decode($apiRequest->getBody()->getContents());

for updates (PATCH) I have to first read the etag of the reccord (@odata.etag), and add on the headers (If-Match value) for update:

 $client = new GuzzleHttpClient();
 $uri=env('HTTP_URIBASE', '');
 // get the recordset of the customer
 $apiRequest = $client->request('GET', $uri.'/Customer(No='.$identifier.')',[
            'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ]     
            ]);
 $content = json_decode($apiRequest->getBody()->getContents());
 $etag= $content->{'@odata.etag'};

 // update description of the customer
 $apiRequest = $client->request('PATCH', $uri.'/Customer(No='.$identifier.')',[
        'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ],
        'headers' => ['Content-Type' => 'application/json', 
                       'Accept' => 'application/json',
                       'If-Match' =>$etag ],
        'body'    => '{"Name":"'.$missatge.'"}' 
         ]);
 $content = json_decode($apiRequest->getBody()->getContents());
于 2017-12-09T22:28:18.913 回答