简体   繁体   中英

Querying Microsoft Dynamics NAV 2013 Odata service as JSON Format

i have read here , that the Odata Webservice also supports the JSON format. But how can I get that?

When I send a request i only get the following format> application/atom+xml

Try something like that:

$.ajax({
       type: "GET",
       contentType: "application/json; charset=utf-8",
       datatype: "json",
       url: odataSelect,
       beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
       success: function (data, textStatus, XmlHttpRequest) 
           { 
               ProcessReturnedEntities(data.d.results); 
               ProcessReturnedEntity(data.d);
           },
       error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
   });

See this site for a complete example.

For WinJS inside Windows 8 apps with HTML & JS its the following:

WinJS.xhr({
    type: "GET",
    datatype: "json",
    url: 'http://localhost:7048/DynamicsNAV70/OData/P_21/',
    headers: {
        "Content-type": "application/json; charset=utf-8", "Accept": "application/json" },
    }).done(function (data, textStatus, XmlHttpRequest)  {
         console.log();
    },
    function (err) {
        console.log();
    });

Please note the different definition of the header. The values are exactly the same.

To communicate with your OData web services using JSON instead of XML, you really only need to set the following two headers :

  • Accept: application/json
  • Content-Type: application/json; charset=utf-8

Alternatively, you could also put ?$format=json at the end of your URL.

This it true no matter what programming language you're using to communicate with Microsoft Dynamics NAV. It works the same for JavaScript, JAVA, Python, Ruby, PHP, ...


Demo code

Here's how to do a basic GET request from PHP :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'1\')'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, false);  

curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);
// Close handle
curl_close($ch);

Here's how to do a basic POST request from PHP :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "Name" => "This is a test customer",
    ...
]));

curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);
curl_close($ch);

Here's how to do a basic PATCH request from PHP :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'1\')'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "Name" => "This is a test customer",
    ...
]));

curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',
        'If-Match: W/"\'' .  $etag . '\'"'
        // You can get your etag value by doing a get request first
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);
curl_close($ch);

Here's how to do a basic DELETE request from PHP :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'1\')'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

curl_setopt($ch, CURLOPT_USERPWD, 'username:password');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',
        'If-Match: W/"\'' .  $etag . '\'"'
        // You can get your etag value by doing a get request first
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);
curl_close($ch);

Note 1

If you need to create / update data, don't forget to Json-encode your POST fields :

curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "Name"=> "This is the name of my new customer"
]));

Using a query string or array instead will generate an error An error occurred while processing this request. , which could leave you puzzled for quite a while...


Note 2

For those who don't like working with raw cURL requests, I just uploaded a basic OO wrapper class, which you can find at this gist .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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