2.2.7.1.1.1 Examples
See Appendix A: Sample Entity Data Model and CSDL Document (section 6) for the sample model and data used in this section.
Example 1: Insert a new Customer and bind it to existing Orders with key values 1 and 2 by using the Atom format.
-
HTTP Request:
-
POST /service.svc/Customers HTTP/1.1 Host: host Content-Type: application/atom+xml DataServiceVersion: 1.0 Accept: application/atom+xml Content-Length: nnn <?xml version="1.0" encoding="utf-8"?> <entry xml:base="http://host/service.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <id>http://host/service.svc/Customers('ASDFG')</id> <title type="text" /> <updated>2008-12-07T8:00:00Z</updated> <author> <name /> </author> <link rel="http://schemas.microsoft.com/ado/2007/08 /dataservices/related/Orders" href="Orders(1)" /> <link rel="http://schemas.microsoft.com/ado/2007/08 /dataservices/related/Orders" href="Orders(2)" /> <content type="application/xml"> <m:properties> <d:CustomerID>ASDFG</d:CustomerID> <d:CompanyName>Contoso Widgets</d:CompanyName> <d:Address> <d:Street>58 Contoso St</d:Street> <d:City>Seattle</d:City> </d:Address> </m:properties> </content> </entry> -
HTTP Response:
-
HTTP/1.1 201 Created Date: Fri, 12 Dec 2008 17:17:11 GMT Location: http://host/service.svc/Customers('ASDFG') Content-Type: application/atom+xml;type=entry DataServiceVersion: 3.0 Content-Length: nnn ETag: W/"X'000000000000FA01'" <?xml version="1.0" encoding="utf-8"?> <entry xml:base="http://host/service.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <category term="SampleModel.Customer" scheme="http://schemas.microsoft.com/ado/2007/08 /dataservices/scheme"/> <id>http://host/service.svc/Customers('ASDFG')</id> <title type="text" /> <updated>2008-03-30T21:32:23Z</updated> <author> <name /> </author> <link rel="edit" title="Customers" href="Customers('ASDFG')" /> <link rel="http://schemas.microsoft.com/ado/2007/08 /dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('ASDFG')/Orders" /> xmlns:d="http://schemas.microsoft.com/ado/2007/08 /dataservices/relatedlinks /dataservices/related/Orders" type="application/xml" title="Orders" href="Customers('ASDFG')/$links/Orders" /> <content type="application/xml"> <m:properties> <d:CustomerID>ASDFG</d:CustomerID> <d:CompanyName>Contoso Widgets</d:CompanyName> <d:Address> <d:Street>58 Contoso St</d:Street> <d:City>Seattle</d:City> </d:Address> <d:Version>AAAAAAAA+gE=</d:Version> </m:properties> </content> </entry>
Example 2: Insert a new Customer and bind it to existing Orders with key values 1 and 2 by using the Verbose JSON format.
-
HTTP Request:
-
POST /service.svc/Customers HTTP/1.1 Host: host Content-Type: application/json;odata=verbose Accept: application/json;odata=verbose DataServiceVersion: 1.0 Content-Length: nnn { "__metadata": { "uri": "Customers(\'ASDFG\')" }, "CustomerID": "ASDFG", "CompanyName": "Contoso Widgets", "Address": { "Street": "58 Contoso St", "City": "Seattle" }, "Orders": [ { "__metadata": {"uri": "Order(1)"} }, { "__metadata": {"uri": "Order(2)"} } ] } -
HTTP Response (Version 3.0):
-
HTTP/1.1 201 Created Date: Fri, 12 Dec 2008 17:17:11 GMT Location: http://host/service.svc/Customers('ASDFG') Content-Type: application/json;odata=verbose DataServiceVersion: 3.0 Content-Length: nnn ETag: W/"X'000000000000FA01'" { "d": { "__metadata": { "uri": "Customers(\'ASDFG\')", "type": "SampleModel.Customer", "etag": "W/\"X\'000000000000FA01\'\"", "properties" : { "Orders" : { "associationuri" : "Customers(\'ASDFG\')/$links/Orders" } } }, "CustomerID": "ASDFG", "CompanyName": "Contoso Widgets", "Address": { "Street": "58 Contoso St", "City": "Seattle" }, "Version": "AAAAAAAA+gE=", "Orders": { "__deferred": { "uri": "Customers(\'ASDFG\')/Orders"} } } }
Example 3: Insert a new Customer and two new related orders by using the Atom format.
-
HTTP Request:
-
POST /service.svc/Customers HTTP/1.1 Host: host Content-Type: application/atom+xml DataServiceVersion: 1.0 Accept: application/atom+xml Content-Length: nnn <?xml version="1.0" encoding="utf-8"?> <entry xml:base="http://host/service.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <id>http://host/service.svc/Customers('ASDFG')</id> <title type="text" /> <updated>2008-03-30T21:52:23Z</updated> <author> <name /> </author> <link rel="http://schemas.microsoft.com/ado/2007/08 /dataservices/related/Orders" href="Customers('ASDFG')/Orders"> <m:inline> <feed> <title type="text">Orders</title> <id>http://host/service.svc/Customers('ASDFG')/Orders</id> <updated>2008-03-30T21:52:46Z</updated> <link rel="self" title="Orders" href="Customers('ASDFG')/Orders" /> <entry> <id>http://host/service.svc/Orders(3)</id> <title type="text" /> <updated>2008-03-30T21:52:45Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:OrderID>3</d:OrderID> <d:ShippedDate> 2008-03-30T21:52:45Z</d:ShippedDate> </m:properties> </content> </entry> <entry> <id>http://host/service.svc/Orders(4)</id> <title type="text" /> <updated>2008-03-30T21:52:45Z</updated> <author> <name /> </author> <content type="application/xml"> <m:properties> <d:OrderID>4</d:OrderID> <d:ShippedDate>2008-03-30T21:52:45Z</d:ShippedDate> </m:properties> </content> </entry> </feed> </m:inline> </link> <content type="application/xml"> <m:properties> <d:CustomerID>ASDFG</d:CustomerID> <d:CompanyName>Contoso Widgets</d:CompanyName> <d:Address> <d:Street>58 Contoso St</d:Street> <d:City>Seattle</d:City> </d:Address> </m:properties> </content> </entry> -
HTTP Response (Version 3.0):
-
HTTP/1.1 201 Created Date: Fri, 12 Dec 2008 17:17:11 GMT Location: http://host/service.svc/Customers('ASDFG') Content-Type: application/atom+xml;type=entry DataServiceVersion: 3.0 Content-Length: nnn ETag: W/"X'000000000000FA01'" <?xml version="1.0" encoding="utf-8"?> <entry xml:base="http://host/service.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <category term="SampleModel.Customer" scheme="http://schemas.microsoft.com/ado/2007/08 /dataservices/scheme"/> <id>http://host/service.svc/Customers('ASDFG')</id> <title type="text" /> <updated>2008-03-30T21:32:23Z</updated> <author> <name /> </author> <link rel="edit" title="Customers" href="Customers('ASDFG')" /> <link rel="http://schemas.microsoft.com/ado/2007/08 /dataservices/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="Customers('ASDFG')/Orders" /> <link rel="http://schemas.microsoft.com/ado/2007/08 /dataservices/relatedlinks/Orders" type="application/xml" title="Orders" href="Customers('ASDFG')/$links/Orders" /> <content type="application/xml"> <m:properties> <d:CustomerID>ASDFG</d:CustomerID> <d:CompanyName>Contoso Widgets</d:CompanyName> <d:Address> <d:Street>58 Contoso St</d:Street> <d:City>Seattle</d:City> </d:Address> <d:Version m:type="Edm.Binary">AAAAAAAA+gE=</d:Version> </m:properties> </content> </entry>
Example 4: Insert a new Customer and two new related orders by using the Verbose JSON.
-
HTTP Request:
-
POST /service.svc/Customers HTTP/1.1 Host: host Content-Type: application/json;odata=verbose Accept: application/json;odata=verbose DataServiceVersion: 1.0 Content-Length: nnn { "CustomerID": "ASDFG", "CompanyName": "Contoso Widgets", "Address": { "Street": "58 Contoso St", "City": "Seattle" }, "Orders": [ { "OrderID": 1, "ShippedDate": "\/Date(872467200000)\/" }, { "OrderID": 2, "ShippedDate": "\/Date(875836800000)\/" } ] } -
HTTP Response (Version 3.0):
-
HTTP/1.1 201 Created Date: Fri, 12 Dec 2008 17:17:11 GMT Location: http://host/service.svc/Customers('ASDFG') Content-Type: application/json;odata=verbose DataServiceVersion: 3.0 Content-Length: nnn ETag: W/"X'000000000000FA01'" {"d": { "__metadata": { "uri": "Customers(\'ASDFG\')", "type": "SampleModel.Customer", "etag": "W/\"X\'000000000000FA01\'\"", "properties" : { "Orders" : { "associationuri" : "Customers(\'ASDFG\')/$links/Orders" } } }, "CustomerID": "ASDFG", "CompanyName": "Contoso Widgets", "Address": { "Street": "58 Contoso St", "City": "Seattle" }, "Version": "AAAAAAAA+gE=", "Orders": { "__deferred": { "uri": "Customers(\'ASDFG\')/Orders"} } } }