Create Cosmos Document
This tutorial talks about how to create documents in cosmos using the cosmos API. We can use Document APIs to manage or create document in Cosmos DB collection. You can refer to the link for cosmos APIs – cosomos API
Authorization behavior for all the calls will be similar but we have to change verb as per our need, so we have to update Dataweave and HTTP request call to Azure.
Mule flow will look like this
Dataweave
%dw 2.0 import java!cosmosdb::GenerateCosmosDBAuthToken output application/java var verb = "POST" var resourceType = "docs" var resourceId = "dbs/hr/colls/employee" var key = "0SZd1Zs3hySH915hKBTQCbewWnzIEI4S2YK8yHDaRwMrnzrZSwaa0hHy8IW21x4qbsQoswWIh3FW8TOd0fpSYw==" var keyType = "master" var tokenVersion = "1.0" var serverDateTime = GenerateCosmosDBAuthToken::getServerTime() as String --- { serverDateTime: serverDateTime, authKey: GenerateCosmosDBAuthToken::generate(verb, resourceType, resourceId, key, keyType, tokenVersion, serverDateTime) }
Please change the resource id and key as per your Azure details
HTTP request
<http:request method="POST" doc:name="Cosmos DB API for SQL insert" doc:id="d1b8eda4-8576-48b2-ab08-c27bfa422779" config-ref="HTTP_Request_configuration" path="dbs/hr/colls/employee/docs"> <http:headers ><![CDATA[#[output application/java --- { "Accept" : "application/json", "x-ms-version" : "2016-07-11", "Authorization" : vars.cosmosHeader.authKey, "x-ms-date" : vars.cosmosHeader.serverDateTime, "Content-Type" : "application/json", "x-ms-documentdb-partitionkey" : '["' ++ payload.department ++ '"]' }]]]></http:headers> </http:request>
Please see the path and different HTTP header we are passing in HTTP request call, please change the same as per your need
Test it through SOAP UI
Documents in Azure
Sample application – sample application
Sample SOAP UI project – Soap UI project