Friday, August 4, 2017

Accessing Dynamics 365 for Operations ODATA services with Fiddler

Before you start, you will need to do a new application registration first through Azure portal to get the Client ID and the Client Secret key.

After that, download and install Fiddler from http://www.telerik.com/fiddler

Open Fiddler and go to the Composer [tab] and Options [tab]
Enable "Inspect session" and "Fix content-length header"

Then open the Scratchpad [tab] and paste this

POST https://login.windows.net/<tenant>/oauth2/token HTTP/1.1 
Content-Type: application/x-www-form-urlencoded
Host: login.windows.net

resource=https://<axurl>&client_id=<client-id>&client_secret=<client-secret>&grant_type=client_credentials

Replace the <tenant>, <axurl>, <client-id> and <client-secret> with the valid values, then select/highlight the statements and click the Execute button
The view should be switched to the Inspector [tab] and you can click on the Raw [tab] to see the raw result.
Copy the value of the access_token as you will need this for the subsequent service calls.


If you don't have the client secret key, but you have the username and password, you can use this to get the token:
POST https://login.windows.net/<tenant>/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: login.windows.net

resource=https://<axurl>&client_id=<client-id>&authorityURL=https://login.windows.net/<tenant>&username=<username>&password=<password>&grant_type=password
Replace the <tenant>, <axurl>, <client-id>, <username> and <password> with the valid values, then select/highlight the statements and click the Execute button
The view should be switched to the Inspector [tab] and you can click on the Raw [tab] to see the raw result.
Copy the value of the access_token as you will need this for the subsequent service calls.


After we get the access token, let's try to call the LedgerJournalHeaders service.

To do a GET call:

GET https://<axurl>/data/LedgerJournalHeaders HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
Host: <axurl>

Replace the <axurl> and <access_token> with the valid values, then select/highlight the statements and click the Execute button.
The view should be switched to the Inspector [tab] and you can click on the JSON [tab] to see the result.

To insert a new journal header:

POST https://<axurl>/data/LedgerJournalHeaders HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
Host: <axurl>
{
"@odata.type":"#Microsoft.Dynamics.DataEntities.LedgerJournalHeader",
"dataAreaId":"USMF",
"JournalName":"GenJrn",
"Description":"Test journal"
}

Replace the <axurl> and <access_token> with the valid values, then select/highlight the statements and click the Execute button.
The view should be switched to the Inspector [tab], if the insert is successful then it will return the newly inserted record as the result.


To update a journal header:

PATCH https://<axurl>/data/LedgerJournalHeaders(JournalBatchNumber='<journalNumber>',dataAreaId='<dataAreaId>') HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
Host: <axurl>
{
"@odata.type":"#Microsoft.Dynamics.DataEntities.LedgerJournalHeader",
"dataAreaId":"USMF",
"Description":"Edited journal description"
}

Replace the <axurl>, <access_token>, <journalNumber> and <dataAreaId> with the valid values, then select/highlight the statements and click the Execute button.
Please note that D365O by default will get the values from the integration user's default company, regardless of the dataAreaId that you're actually specifying on the call. If you need to access a record on a different company, then you'll need to add ?cross-company=true

If the update is successful then it will return HTTP status 204, otherwise it will return HTTP status 400 with the error message.


To delete a journal header:

DELETE https://<axurl>/data/LedgerJournalHeaders(JournalBatchNumber='<journalNumber>',dataAreaId='<dataAreaId>') HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
Host: <axurl>

Replace the <axurl>, <access_token>, <journalNumber> and <dataAreaId> with the valid values, then select/highlight the statements and click the Execute button.
If the update is successful then it will return HTTP status 204, otherwise it will return HTTP status 400 with the error message.


To insert multiple journal headers in one request:

POST https://<axurl>/data/$batch HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: multipart/mixed; boundary=batch_boundary
Accept: multipart/mixed
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
Host: <axurl>
 
--batch_boundary
Content-Type: multipart/mixed; boundary=changeset_boundary
 
--changeset_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1
POST https://<axurl>/data/LedgerJournalHeaders HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
{
"@odata.type":"#Microsoft.Dynamics.DataEntities.LedgerJournalHeader",
"dataAreaId":"USMF",
"JournalName":"GenJrn",
"Description":"Test journal 1"
}
 
--changeset_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2
POST https://<axurl>/data/LedgerJournalHeaders HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
{
"@odata.type":"#Microsoft.Dynamics.DataEntities.LedgerJournalHeader",
"dataAreaId":"USMF",
"JournalName":"GenJrn",
"Description":"Test journal 2"
}


To call an ODATA action:

POST https://<axurl>/data/<EntityPublicCollectionName>([EntityKey])/Microsoft.Dynamics.DataEntities.<ActionName> HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Content-Type: application/json
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
Authorization: Bearer <access_token>
Host: <axurl>



As you can see, Fiddler can be used as a simple tool to access the D365O services. I think this is easier and faster than having to build custom codes in Visual Studio to do the calls.
However depending how many tests that you'll need to perform, it can be a bit painful to copy paste the access token values for every call.

Credit to Kalle Sõber on his post http://www.k3technical.com/testing-ax7-odata-services-fiddler/ 

In the next post, I will show how to use Postman to call D365O services. Postman has variable system that makes it easier to do the calls so that you don't have to copy paste the access token, like we did just now with Fiddler.

No comments:

Post a Comment