本文转自:http://www.odata.org/getting-started/basic-tutorial/

Basic Tutorial

The Open Data Protocol (OData) is a data access protocol built on core protocols like HTTP and commonly accepted methodologies like REST for the web. There are various kinds of libraries and tools  can be used to consume OData services. But for beginners and those who want to write their own libraries, the pure HTTP requests and responses are also very important. This documentation will not cover every feature details for OData V4 services but will try to cover various typical scenarios.  If you want to have a more detailed understanding, please refer to OData Documentation.

Pleae be noted

  • This is the Basic section. For topics like Singleton, Inheritance, Open Type, Containment and Batch, please refer to Advanced Section.
  • All these are based on OData V4 sample service TripPin, please just replace the serviceRoot below with the URL http://services.odata.org/V4/TripPinServiceRW. TripPin now supports ETag, so for data modification topics, you should first using GET on TripPin (eg. GET serviceRoot/People) to get the section information in the payload then use the URL with section for data modification examples. (something looks like http://services.odata.org/V4/(S(flat4rgegiueineatspfdku1))/TripPinServiceRW)
  • You may use Fiddler to run the request (especially the complex ones of data modification) and get the JSON response.
  • In TripPin service, ETag is currently enabled on entity Person. We omit the ETag related headers in request headers for short, please go to the ETag Section for detailed information.
  • We will keep improving this by adding contents and fixing bugs. You can provide your feedbacks and ask questions using OData Mailling List.

Requesting Data

OData services support requests for data via HTTP GET requests.

Requesting Entity Collections

The request below returns the the collection of Person People. GET serviceRoot/People
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "@odata.nextLink": "serviceRoot/People?%24skiptoken=8",
  4. "value": [
  5. {
  6. "@odata.id": "serviceRoot/People('russellwhyte')",
  7. "@odata.etag": "W/\"08D1694BD49A0F11\"",
  8. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  9. "UserName": "russellwhyte",
  10. "FirstName": "Russell",
  11. "LastName": "Whyte",
  12. "Emails": [
  13. "Russell@example.com",
  14. "Russell@contoso.com"
  15. ],
  16. "AddressInfo": [
  17. {
  18. "Address": "187 Suffolk Ln.",
  19. "City": {
  20. "CountryRegion": "United States",
  21. "Name": "Boise",
  22. "Region": "ID"
  23. }
  24. }
  25. ],
  26. "Gender": "Male",
  27. "Concurrency": 635404796846280400
  28. },
  29. ......
  30. ,
  31. {
  32. "@odata.id": "serviceRoot/People('keithpinckney')",
  33. "@odata.etag": "W/\"08D1694BD49A0F11\"",
  34. "@odata.editLink": "serviceRoot/People('keithpinckney')",
  35. "UserName": "keithpinckney",
  36. "FirstName": "Keith",
  37. "LastName": "Pinckney",
  38. "Emails": [
  39. "Keith@example.com",
  40. "Keith@contoso.com"
  41. ],
  42. "AddressInfo": [],
  43. "Gender": "Male",
  44. "Concurrency": 635404796846280400
  45. }
  46. ]
  47. }

Requesting an Individual Entity by ID

The request below returns an individual entity of type Person by the given UserName “russellwhyte” GET serviceRoot/People('russellwhyte')
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People/$entity",
  3. "@odata.id": "serviceRoot/People('russellwhyte')",
  4. "@odata.etag": "W/\"08D1694BF26D2BC9\"",
  5. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  6. "UserName": "russellwhyte",
  7. "FirstName": "Russell",
  8. "LastName": "Whyte",
  9. "Emails": [
  10. "Russell@example.com",
  11. "Russell@contoso.com"
  12. ],
  13. "AddressInfo": [
  14. {
  15. "Address": "187 Suffolk Ln.",
  16. "City": {
  17. "CountryRegion": "United States",
  18. "Name": "Boise",
  19. "Region": "ID"
  20. }
  21. }
  22. ],
  23. "Gender": "Male",
  24. "Concurrency": 635404797346655200
  25. }

Requesting an Individual Property

To address an entity property clients append a path segment containing property name to the URL of the entity. If the property has a complex type, properties of that value can be addressed by further property name composition.                 First let’s take a look at how to get a simple property. The request below returns the Name property of an Airport.

GET serviceRoot/Airports('KSFO')/Name
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#Airports('KSFO')/Name",
  3. "value": "San Francisco International Airport"
  4. }

Then let’s see how to get a property value of a complex type. The request below returns the Address of the complex type Location in an Airport. GET serviceRoot/Airports('KSFO')/Location/Address
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#Airports('KSFO')/Location/Address",
  3. "value": "South McDonnell Road, San Francisco, CA 94128"
  4. }

Requesting an Individual Property Raw Value

To address the raw value of a primitive property, clients append a path segment containing the string $value to the property URL. The request below returns the raw value of property Name of an Airport. GET serviceRoot/Airports('KSFO')/Name/$value
                Response Payload

  1. San Francisco International Airport

Querying Data

OData supports various kinds of query options for querying data. This section will help you go through the common scenarios for these query options.

System Query Option $filter

The $filter system query option allows clients to filter a collection of resources that are addressed by a request URL. The expression specified with $filter is evaluated for each resource in the collection, and only items where the expression evaluates to true are included in the response.

Basic predicates, built-in functions

There are several kinds of basic predicates and built-in functions for $filter, including logical operators and arithmetic operators. For more detailed information, please refer to OData V4 URL Conventions Document. The request below using $filter to get people with FirstName “Scott”. GET serviceRoot/People?$filter=FirstName eq 'Scott'
                    Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/People('scottketchum')",
  6. "@odata.etag": "W/\"08D1694C7E510464\"",
  7. "@odata.editLink": "serviceRoot/People('scottketchum')",
  8. "UserName": "scottketchum",
  9. "FirstName": "Scott",
  10. "LastName": "Ketchum",
  11. "Emails": [
  12. "Scott@example.com"
  13. ],
  14. "AddressInfo": [
  15. {
  16. "Address": "2817 Milton Dr.",
  17. "City": {
  18. "CountryRegion": "United States",
  19. "Name": "Albuquerque",
  20. "Region": "NM"
  21. }
  22. }
  23. ],
  24. "Gender": "Male",
  25. "Concurrency": 635404799693620400
  26. }
  27. ]
  28. }

Filter on Complex Type

$filter can also work on complex type. The request below returns airports with “San Francisco” contained in its Address. And Address is property of complex type Location.

GET serviceRoot/Airports?$filter=contains(Location/Address, 'San Francisco')
                    Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#Airports",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/Airports('KSFO')",
  6. "@odata.editLink": "serviceRoot/Airports('KSFO')",
  7. "IcaoCode": "KSFO",
  8. "Name": "San Francisco International Airport",
  9. "IataCode": "SFO",
  10. "Location": {
  11. "Address": "South McDonnell Road, San Francisco, CA 94128",
  12. "City": {
  13. "CountryRegion": "United States",
  14. "Name": "San Francisco",
  15. "Region": "California"
  16. },
  17. "Loc": {
  18. "type": "Point",
  19. "coordinates": [
  20. -122.374722222222,
  21. 37.6188888888889
  22. ],
  23. "crs": {
  24. "type": "name",
  25. "properties": {
  26. "name": "EPSG:4326"
  27. }
  28. }
  29. }
  30. }
  31. }
  32. ]
  33. }

Filter on Enum Properties

The request below returns all female People of entity type Person. The Gender is a property of Enum type. GET serviceRoot/People?$filter=Gender eq Microsoft.OData.SampleService.Models.TripPin.PersonGender'Female'
                    Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "@odata.nextLink": "serviceRoot/People?%24filter=Gender+eq+Microsoft.OData.SampleService.Models.TripPin.PersonGender%27Female%27&%24skiptoken=8",
  4. "value": [
  5. {
  6. "@odata.id": "serviceRoot/People('elainestewart')",
  7. "@odata.etag": "W/\"08D1694CCFB34453\"",
  8. "@odata.editLink": "serviceRoot/People('elainestewart')",
  9. "UserName": "elainestewart",
  10. "FirstName": "Elaine",
  11. "LastName": "Stewart",
  12. "Emails": [
  13. "Elaine@example.com",
  14. "Elaine@contoso.com"
  15. ],
  16. "AddressInfo": [],
  17. "Gender": "Female",
  18. "Concurrency": 635404801059013800
  19. },
  20. ......
  21. ,
  22. {
  23. "@odata.id": "serviceRoot/People('ursulabright')",
  24. "@odata.etag": "W/\"08D1694CCFB34453\"",
  25. "@odata.editLink": "serviceRoot/People('ursulabright')",
  26. "UserName": "ursulabright",
  27. "FirstName": "Ursula",
  28. "LastName": "Bright",
  29. "Emails": [
  30. "Ursula@example.com",
  31. "Ursula@contoso.com"
  32. ],
  33. "AddressInfo": [],
  34. "Gender": "Female",
  35. "Concurrency": 635404801059013800
  36. }
  37. ]
  38. }

Nested Filter in Expand

OData V4 supports nested filters in $expand. The request below return People and all their trips with Name “Trip in US”. GET serviceRoot/People?$expand=Trips($filter=Name eq 'Trip in US')
                    Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "@odata.nextLink": "serviceRoot/People?%24expand=Trips(%24filter%3dName+eq+%27Trip+in+US%27)&%24skiptoken=8",
  4. "value": [
  5. {
  6. "@odata.id": "serviceRoot/People('russellwhyte')",
  7. "@odata.etag": "W/\"08D1694D0DBBD9DB\"",
  8. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  9. "UserName": "russellwhyte",
  10. "FirstName": "Russell",
  11. "LastName": "Whyte",
  12. "Emails": [
  13. "Russell@example.com",
  14. "Russell@contoso.com"
  15. ],
  16. "AddressInfo": [
  17. {
  18. "Address": "187 Suffolk Ln.",
  19. "City": {
  20. "CountryRegion": "United States",
  21. "Name": "Boise",
  22. "Region": "ID"
  23. }
  24. }
  25. ],
  26. "Gender": "Male",
  27. "Concurrency": 635404802099763700,
  28. "Trips@odata.context": "serviceRoot/$metadata#People('russellwhyte')/Trips",
  29. "Trips": [
  30. {
  31. "TripId": 1001,
  32. "ShareId": "9d9b2fa0-efbf-490e-a5e3-bac8f7d47354",
  33. "Description": "Trip from San Francisco to New York City",
  34. "Name": "Trip in US",
  35. "Budget": 3000,
  36. "StartsAt": "2014-01-01T00:00:00Z",
  37. "EndsAt": "2014-01-04T00:00:00Z",
  38. "Tags": [
  39. "business",
  40. "New York meeting"
  41. ]
  42. }
  43. ]
  44. },
  45. ......
  46. ,
  47. {
  48. "@odata.id": "serviceRoot/People('keithpinckney')",
  49. "@odata.etag": "W/\"08D1694D0DBBD9DB\"",
  50. "@odata.editLink": "serviceRoot/People('keithpinckney')",
  51. "UserName": "keithpinckney",
  52. "FirstName": "Keith",
  53. "LastName": "Pinckney",
  54. "Emails": [
  55. "Keith@example.com",
  56. "Keith@contoso.com"
  57. ],
  58. "AddressInfo": [],
  59. "Gender": "Male",
  60. "Concurrency": 635404802099763700,
  61. "Trips@odata.context": "serviceRoot/$metadata#People('keithpinckney')/Trips",
  62. "Trips": []
  63. }
  64. ]
  65. }

System Query Option $orderby

The $orderby system query option allows clients to request resources in either ascending order using asc or descending order using desc. If asc or desc not specified, then the resources will be ordered in ascending order. The request below orders Trips on property EndsAt in descending order.

GET serviceRoot/People('scottketchum')/Trips?$orderby=EndsAt desc
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People('scottketchum')/Trips",
  3. "value": [
  4. {
  5. "TripId": 2004,
  6. "ShareId": "f94e9116-8bdd-4dac-ab61-08438d0d9a71",
  7. "Description": "Trip from Shanghai to Beijing",
  8. "Name": "Trip in Beijing",
  9. "Budget": 11000,
  10. "StartsAt": "2014-02-01T00:00:00Z",
  11. "EndsAt": "2014-02-04T00:00:00Z",
  12. "Tags": [
  13. "Travel",
  14. "Beijing"
  15. ]
  16. },
  17. {
  18. "TripId": 2002,
  19. "ShareId": "9d9b2fa0-efbf-490e-a5e3-bac8f7d47354",
  20. "Description": "Trip from San Francisco to New York City",
  21. "Name": "Trip in US",
  22. "Budget": 5000,
  23. "StartsAt": "2014-01-01T00:00:00Z",
  24. "EndsAt": "2014-01-04T00:00:00Z",
  25. "Tags": [
  26. "business",
  27. "New York meeting"
  28. ]
  29. }
  30. ]
  31. }

System Query Option $top and $skip

The $top system query option requests the number of items in the queried collection to be included in the result. The $skip query option requests the number of items in the queried collection that are to be skipped and not included in the result.                 The request below returns the first two people of the People entity set.

GET serviceRoot/People?$top=2
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/People('russellwhyte')",
  6. "@odata.etag": "W/\"08D1694D4AA4A8D1\"",
  7. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  8. "UserName": "russellwhyte",
  9. "FirstName": "Russell",
  10. "LastName": "Whyte",
  11. "Emails": [
  12. "Russell@example.com",
  13. "Russell@contoso.com"
  14. ],
  15. "AddressInfo": [
  16. {
  17. "Address": "187 Suffolk Ln.",
  18. "City": {
  19. "CountryRegion": "United States",
  20. "Name": "Boise",
  21. "Region": "ID"
  22. }
  23. }
  24. ],
  25. "Gender": "Male",
  26. "Concurrency": 635404803121654000
  27. },
  28. {
  29. "@odata.id": "serviceRoot/People('scottketchum')",
  30. "@odata.etag": "W/\"08D1694D4AA4A8D1\"",
  31. "@odata.editLink": "serviceRoot/People('scottketchum')",
  32. "UserName": "scottketchum",
  33. "FirstName": "Scott",
  34. "LastName": "Ketchum",
  35. "Emails": [
  36. "Scott@example.com"
  37. ],
  38. "AddressInfo": [
  39. {
  40. "Address": "2817 Milton Dr.",
  41. "City": {
  42. "CountryRegion": "United States",
  43. "Name": "Albuquerque",
  44. "Region": "NM"
  45. }
  46. }
  47. ],
  48. "Gender": "Male",
  49. "Concurrency": 635404803121654000
  50. }
  51. ]
  52. }

The request below returns people starting with the 19th people of the entity set People

GET serviceRoot/People?$skip=18
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/People('genevievereeves')",
  6. "@odata.etag": "W/\"08D1694D8B408D60\"",
  7. "@odata.editLink": "serviceRoot/People('genevievereeves')",
  8. "UserName": "genevievereeves",
  9. "FirstName": "Genevieve",
  10. "LastName": "Reeves",
  11. "Emails": [
  12. "Genevieve@example.com",
  13. "Genevieve@contoso.com"
  14. ],
  15. "AddressInfo": [],
  16. "Gender": "Female",
  17. "Concurrency": 635404804205612400
  18. },
  19. {
  20. "@odata.id": "serviceRoot/People('kristakemp')",
  21. "@odata.etag": "W/\"08D1694D8B408D60\"",
  22. "@odata.editLink": "serviceRoot/People('kristakemp')",
  23. "UserName": "kristakemp",
  24. "FirstName": "Krista",
  25. "LastName": "Kemp",
  26. "Emails": [
  27. "Krista@example.com"
  28. ],
  29. "AddressInfo": [],
  30. "Gender": "Female",
  31. "Concurrency": 635404804205612400
  32. }
  33. ]
  34. }

System Query Option $count

The $count system query option allows clients to request a count of the matching resources included with the resources in the response.                 The request below returns the total number of people in the collection.

GET serviceRoot/People/$count
                Response Payload

  1. 20

System Query Option $expand

The $expand system query option specifies the related resources to be included in line with retrieved resources. The request below returns people with navigation property Friends of a Person

GET serviceRoot/People('keithpinckney')?$expand=Friends
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People/$entity",
  3. "@odata.id": "serviceRoot/People('keithpinckney')",
  4. "@odata.etag": "W/\"08D1694E2BB4317A\"",
  5. "@odata.editLink": "serviceRoot/People('keithpinckney')",
  6. "UserName": "keithpinckney",
  7. "FirstName": "Keith",
  8. "LastName": "Pinckney",
  9. "Emails": [
  10. "Keith@example.com",
  11. "Keith@contoso.com"
  12. ],
  13. "AddressInfo": [],
  14. "Gender": "Male",
  15. "Concurrency": 635404806897545600,
  16. "Friends": [
  17. {
  18. "@odata.id": "serviceRoot/People('clydeguess')",
  19. "@odata.etag": "W/\"08D1694E2BB4317A\"",
  20. "@odata.editLink": "serviceRoot/People('clydeguess')",
  21. "UserName": "clydeguess",
  22. "FirstName": "Clyde",
  23. "LastName": "Guess",
  24. "Emails": [
  25. "Clyde@example.com"
  26. ],
  27. "AddressInfo": [],
  28. "Gender": "Male",
  29. "Concurrency": 635404806897545600
  30. },
  31. {
  32. "@odata.id": "serviceRoot/People('marshallgaray')",
  33. "@odata.etag": "W/\"08D1694E2BB4317A\"",
  34. "@odata.editLink": "serviceRoot/People('marshallgaray')",
  35. "UserName": "marshallgaray",
  36. "FirstName": "Marshall",
  37. "LastName": "Garay",
  38. "Emails": [
  39. "Marshall@example.com",
  40. "Marshall@contoso.com"
  41. ],
  42. "AddressInfo": [],
  43. "Gender": "Male",
  44. "Concurrency": 635404806897545600
  45. }
  46. ]
  47. }

System Query Option $select

The $select system query option allows the clients to requests a limited set of properties for each entity or complex type. The request below returns Name and IcaoCode of all Airports.

GET serviceRoot/Airports?$select=Name, IcaoCode
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#Airports(Name,IcaoCode)",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/Airports('KSFO')",
  6. "@odata.editLink": "serviceRoot/Airports('KSFO')",
  7. "Name": "San Francisco International Airport",
  8. "IcaoCode": "KSFO"
  9. },
  10. ......
  11. ,
  12. {
  13. "@odata.id": "serviceRoot/Airports('KJFK')",
  14. "@odata.editLink": "serviceRoot/Airports('KJFK')",
  15. "Name": "John F. Kennedy International Airport",
  16. "IcaoCode": "KJFK"
  17. }
  18. ]
  19. }

System Query Option $search

The $search system query option restricts the result to include only those entities matching the specified search expression. The definition of what it means to match is dependent upon the implementation. The request below get all People who has ‘Boise’ in their contents.

serviceRoot/People?$search=Boise
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/People('russellwhyte')",
  6. "@odata.etag": "W/\"08D1694E90C0914C\"",
  7. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  8. "UserName": "russellwhyte",
  9. "FirstName": "Russell",
  10. "LastName": "Whyte",
  11. "Emails": [
  12. "Russell@example.com",
  13. "Russell@contoso.com"
  14. ],
  15. "AddressInfo": [
  16. {
  17. "Address": "187 Suffolk Ln.",
  18. "City": {
  19. "CountryRegion": "United States",
  20. "Name": "Boise",
  21. "Region": "ID"
  22. }
  23. }
  24. ],
  25. "Gender": "Male",
  26. "Concurrency": 635404808592855400
  27. }
  28. ]
  29. }

Lambda Operators

OData defines two operators any and all that evaluate a Boolean expression on a collection. They can work on either collection properties or collection of entities.

The request below returns People with Emails containing “ll@contoso.com”. The Emails is a collection of primitive type string.

GET serviceRoot/People?$filter=Emails/any(s:endswith(s, 'contoso.com'))
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/People('russellwhyte')",
  6. "@odata.etag": "W/\"08D1694ECB6F8E7D\"",
  7. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  8. "UserName": "russellwhyte",
  9. "FirstName": "Russell",
  10. "LastName": "Whyte",
  11. "Emails": [
  12. "Russell@example.com",
  13. "Russell@contoso.com"
  14. ],
  15. "AddressInfo": [
  16. {
  17. "Address": "187 Suffolk Ln.",
  18. "City": {
  19. "CountryRegion": "United States",
  20. "Name": "Boise",
  21. "Region": "ID"
  22. }
  23. }
  24. ],
  25. "Gender": "Male",
  26. "Concurrency": 635404809577402000
  27. },
  28. {
  29. "@odata.id": "serviceRoot/People('marshallgaray')",
  30. "@odata.etag": "W/\"08D1694ECB6F8E7D\"",
  31. "@odata.editLink": "serviceRoot/People('marshallgaray')",
  32. "UserName": "marshallgaray",
  33. "FirstName": "Marshall",
  34. "LastName": "Garay",
  35. "Emails": [
  36. "Marshall@example.com",
  37. "Marshall@contoso.com"
  38. ],
  39. "AddressInfo": [],
  40. "Gender": "Male",
  41. "Concurrency": 635404809577402000
  42. }
  43. ]
  44. }

The request below returns the friends of Me who have friends using “Scott” as their FirstName.

GET serviceRoot/Me/Friends?$filter=Friends/any(f:f/FirstName eq 'Scott')
                Response Payload

  1. {
  2. "@odata.context": "serviceRoot/$metadata#People",
  3. "value": [
  4. {
  5. "@odata.id": "serviceRoot/People('russellwhyte')",
  6. "@odata.etag": "W/\"08D1694EE92CB5C3\"",
  7. "@odata.editLink": "serviceRoot/People('russellwhyte')",
  8. "UserName": "russellwhyte",
  9. "FirstName": "Russell",
  10. "LastName": "Whyte",
  11. "Emails": [
  12. "Russell@example.com",
  13. "Russell@contoso.com"
  14. ],
  15. "AddressInfo": [
  16. {
  17. "Address": "187 Suffolk Ln.",
  18. "City": {
  19. "CountryRegion": "United States",
  20. "Name": "Boise",
  21. "Region": "ID"
  22. }
  23. }
  24. ],
  25. "Gender": "Male",
  26. "Concurrency": 635404810076337700
  27. },
  28. {
  29. "@odata.id": "serviceRoot/People('ronaldmundy')",
  30. "@odata.etag": "W/\"08D1694EE92CB5C3\"",
  31. "@odata.editLink": "serviceRoot/People('ronaldmundy')",
  32. "UserName": "ronaldmundy",
  33. "FirstName": "Ronald",
  34. "LastName": "Mundy",
  35. "Emails": [
  36. "Ronald@example.com",
  37. "Ronald@contoso.com"
  38. ],
  39. "AddressInfo": [],
  40. "Gender": "Male",
  41. "Concurrency": 635404810076337700
  42. }
  43. ]
  44. }

Data Modification

Updatable OData services support Create, Update and Delete operation for some or all exposed entities.

Create an Entity

To create an entity in a collection, the client sends a POST request to that collection’s URL. The POST body MUST contain a single valid entity representation. The request below creates a Person which contains complex type and collection property.

                    POST serviceRoot/People                     OData-Version: 4.0                     Content-Type: application/json;odata.metadata=minimal                     Accept: application/json

                    {                     "@odata.type" : "Microsoft.OData.SampleService.Models.TripPin.Person",                     "UserName": "teresa",                     "FirstName" : "Teresa",                     "LastName" : "Gilbert",                     "Gender" : "Female",                     "Emails" : ["teresa@example.com", "teresa@contoso.com"],                     "AddressInfo" : [                     {                     "Address" : "1 Suffolk Ln.",                     "City" :                     {                     "CountryRegion" : "United States",                     "Name" : "Boise",                     "Region" : "ID"                     }                     }]                    }
                Response Payload

  1. HTTP/1.1 201 Created
  2. Content-Length: 468
  3. Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
  4. Location: serviceRoot/People('teresa')
  5. OData-Version: 4.0
  6. {
  7. "@odata.context":"serviceRoot/$metadata#People/$entity",
  8. "@odata.id":"serviceRoot/People('teresa')",
  9. "@odata.editLink":"serviceRoot/People('teresa')",
  10. "UserName":"teresa",
  11. "FirstName":"Teresa",
  12. "LastName":"Gilbert",
  13. "Emails":["teresa@example.com","teresa@contoso.com"],
  14. "AddressInfo":[{"Address":"1 Suffolk Ln.","City":{"CountryRegion":"United States","Name":"Boise","Region":"ID"}}],
  15. "Gender":"Female"
  16. }

Remove an Entity

The request below deletes the Person with UserName ‘vincentcalabrese’. DELETE serviceRoot/People('vincentcalabrese')
                Response Payload

  1. HTTP/1.1 204 No Content
  2. OData-Version: 4.0

Update an Entity

The OData services SHOULD support PATCH as the preferred means of updating an entity. But also services MAY additionally support PUT. The request below update the Emails of a person using PATCH.

                    PATCH serviceRoot/People('russellwhyte')                     OData-Version: 4.0                     Content-Type: application/json;odata.metadata=minimal                     Accept: application/json

                    {                     "@odata.type" : "Microsoft.OData.SampleService.Models.TripPin.Person",                     "Emails" :  ["Russell@example.com", "Russell@contoso.com", "newRussell@contoso.com"]                    }
                Response Payload

  1. HTTP/1.1 204 No Content

Relationship Operations

Relationships from one entity to another are represented as navigation properties.

Link to Related Entities

A successful POST request to a navigation property’s references collection adds a relationship to an existing entity. The request below adds ‘vincentcalabrese’ to friends of ‘scottketchum’. POST serviceRoot/People('scottketchum')/Friends/$ref                         OData-Version: 4.0                         Content-Type: application/json;odata.metadata=minimal                         Accept: application/json

                        {                         "@odata.id": "serviceRoot/People('vincentcalabrese')"                         }
                    Response Payload

  1. HTTP/1.1 204 No Content

Change a Link

A successful PUT request to a single-valued navigation property’s reference resource changes the related entity. The request below change the Airline of a Flight. PUT serviceRoot/People('russellwhyte')/Trips(1001)/PlanItems(11)/Microsoft.OData.SampleService.Models.TripPin.Flight/Airline/$ref                         OData-Version: 4.0                         Content-Type: application/json;odata.metadata=minimal                         Accept: application/json

                        {                         "@odata.id": "serviceRoot/Airlines('FM')"                         }
                    Response Payload

  1. HTTP/1.1 200 OK
  2. {
  3. "@odata.context":"serviceRoot/TripPinServiceRW/$metadata#$ref",
  4. "@odata.id":"serviceRoot/People('russellwhyte')/Trips(1001)/PlanItems(11)/Microsoft.OData.SampleService.Models.TripPin.Flight/Airline"
  5. }

Functions and Actions

OData supports custom operations (Actions and Functions). Functions are operations exposed by an OData service the MUST return data and MUST have no observable side effects. Actions are operations exposed by an OData service that MAY have side effects when invoked. Functions and actions both MAY bound to an entity type, primitive type, complex type, or a collection.

Invoking Unbound Functions

The function below returns the nearest airport with the input geography point. GET serviceRoot/GetNearestAirport(lat = 33, lon = -118)
                    Response Payload

  1. Response 200 OK
  2. {
  3. "@odata.context": "serviceRoot/$metadata#Airports/$entity",
  4. "IcaoCode": "KLAX",
  5. "Name": "Los Angeles International Airport",
  6. "IataCode": "LAX",
  7. "Location": {
  8. "Address": "1 World Way, Los Angeles, CA, 90045",
  9. "City": {
  10. "CountryRegion": "United States",
  11. "Name": "Los Angeles",
  12. "Region": "California"
  13. },
  14. "Loc": {
  15. "type": "Point",
  16. "coordinates": [
  17. -118.408055555556,
  18. 33.9425
  19. ],
  20. "crs": {
  21. "type": "name",
  22. "properties": {
  23. "name": "EPSG:4326"
  24. }
  25. }
  26. }
  27. }
  28. }

Invoking Bound Functions

The request below returns the favorite airline of a person, in TripPin service, “favorite airline” means airline which user choose most times. The function GetFavoriteAirline() is bound to Person. GET serviceRoot/People('russellwhyte')/Microsoft.OData.SampleService.Models.TripPin.GetFavoriteAirline()
                    Response Payload

  1. Response 200 OK
  2. {
  3. "@odata.context": "serviceRoot/$metadata#Airlines/$entity",
  4. "@odata.id": "serviceRoot/Airlines('AA')",
  5. "@odata.editLink": "serviceRoot/Airlines('AA')",
  6. "AirlineCode": "AA",
  7. "Name": "American Airlines"
  8. }

Invoking Unbound Actions

TripPin currently has no scenario supported for unbound actions.

Invoking Bound Actions

The action below shares a trip to one of his friend. In TripPin service, by “share a trip” we mean that the owner and his friend now both have the trip and the trip share the same ShareId property. POST serviceRoot/People('russellwhyte')/Microsoft.OData.SampleService.Models.TripPin.ShareTrip                         OData-Version: 4.0                         Content-Type: application/json;odata.metadata=minimal                         Accept: application/json

                        {                         "userName": "scottketchum",                         "tripId": 1001                         }
                    Response Payload

  1. HTTP/1.1 204 No Content

ETag

OData V4 supports ETag for Data Modification Request and Action Request. This section demonstrates how to operate on entity with ETag enabled. Please be noted that the ETag value below may be out-of-date, so when you try these requests, please first use the GET request to get the ETag of specified entity.

Update Entity with ETag

The request below shows how to update Person which has ETag supported. Please be noted that the value for If-Match should be the same with the ETag value of the specified Person.                         PATCH  serviceRoot/People('clydeguess')                         OData-Version: 4.0                         Content-Type: application/json;odata.metadata=minimal                         Accept: application/json                         If-Match: W/"08D15F3DD9126D84"

                        {                         "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.Person",                         "FirstName" : "CLYDE"                         }
                    Response Payload

  1. HTTP/1.1 204 No Content

Delete Entity with ETag

The request below shows how to delete Person which has ETag supported. Please be noted that the value for If-Match should be the same with the ETag value of the specified Person.                         DELETE serviceRoot/People(‘vincentcalabrese’) OData-Version: 4.0                         Content-Type: application/json;odata.metadata=minimal                         Accept: application/json                         If-Match: W/”08D15F3DD9A61539″
                    Response Payload

  1. HTTP/1.1 204 No Content
 
 

li>a {
display: block;
font-size: 15px;
font-weight: 500;
color: #bf3e11;
padding: 4px 20px;
}
.tutorial-sidebar .nav>li>a:hover, .tutorial-sidebar .nav>li>a:focus {
padding-left: 19px;
color: #563d7c;
text-decoration: none;
background-color: transparent;
border-left: 1px solid #563d7c;
}
.tutorial-sidebar .nav>.active>a, .tutorial-sidebar .nav>.active:hover>a, .tutorial-sidebar .nav>.active:focus>a {
padding-left: 18px;
font-weight: 700;
color: #563d7c;
background-color: transparent;
border-left: 2px solid #563d7c;
}
.tutorial-sidebar .nav .nav {
display: none;
padding-bottom: 10px;
}
.tutorial-sidebar .nav .nav>li>a {
padding-top: 1px;
padding-bottom: 1px;
padding-left: 30px;
font-size: 14px;
font-weight: 400;
}
.tutorial-sidebar .nav .nav>li>a:hover, .tutorial-sidebar .nav .nav>li>a:focus {
padding-left: 29px;
}
.tutorial-sidebar .nav .nav>.active>a, .tutorial-sidebar .nav .nav>.active:hover>a, .tutorial-sidebar .nav .nav>.active:focus>a {
font-weight: 500;
padding-left: 28px;
}
.tutorial-sidebar.affix, .tutorial-sidebar.affix-bottom {
width: 213px;
}
.tutorial-sidebar.affix {
position: fixed;
top: 20px;
}
.tutorial-sidebar.affix-bottom {
position: absolute;
}
.tutorial-sidebar.affix-bottom .tutorial-sidenav, .tutorial-sidebar.affix .tutorial-sidenav {
margin-top: 0;
margin-bottom: 0;
}
.tutorial-sidenav {
margin-top: 20px;
margin-bottom: 20px;
}
@media (min-width:992px) {
.tutorial-sidebar .nav>.active>ul {
display: block;
}

-->

             

© 2014 Open Data Protocol

[转]Open Data Protocol (OData) Basic Tutorial的更多相关文章

  1. Ogre学习笔记Basic Tutorial 前四课总结

    转自:http://blog.csdn.net/yanonsoftware/article/details/1011195 OGRE Homepage:http://www.ogre3d.org/   ...

  2. Ogre1.8.1 Basic Tutorial 6 - The Ogre Startup Sequence

    原文地址:http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Basic+Tutorial+6&structure=Tutorials 1. ...

  3. pytorch例子学习-DATA LOADING AND PROCESSING TUTORIAL

    参考:https://pytorch.org/tutorials/beginner/data_loading_tutorial.html DATA LOADING AND PROCESSING TUT ...

  4. Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)

    ABSTRACT Recent technological advancement have led to a deluge of data from distinctive domains (e.g ...

  5. Protocol Framework - SNMP Tutorial

    30.4 Protocol Framework TCP/IP network management protocols2 divide the management problem into two ...

  6. Resumable Media Uploads in the Google Data Protocol

    Eric Bidelman, Google Apps APIs team February 2010 Introduction The Resumable Protocol Initiating a ...

  7. [cmake] Basic Tutorial

    Basic Project The most basic porject is an executable built from source code file. CMakeLists.txt cm ...

  8. [转]WCF Data Services OData

    http://martinwilley.com/net/data/wcfds.html WCF Data Services About OData Server code Client For .ne ...

  9. Python Data Structure and Algorithms Tutorial

    Python - Algorithm Design - Tutorialspoint https://www.tutorialspoint.com/python_data_structure/pyth ...

随机推荐

  1. PHP 画图——使用jpgraph画图

     1.要支持中文须要用到simhei.ttf和simsun.ttc这两个字体,在使用中文的时候须要使用SetFont(FF_SIMSUN,FS_BOLD)设置字体. 将须要的字体放入到项目文件夹下 ...

  2. Linq To Entities中的动态排序

    换了工作有一个月了,一样的工作.一样的代码.一样的体力活仍就…… Linq To Entityes 也是不新玩意了,近半年来也一直与之打交道,但一直也没对其深究过.今天新加的功能要对所有列支持排序,这 ...

  3. Java中会存在内存泄露吗?请简单描述。

    本文转载自:Java中会存在内存泄漏吗,请简单描述 会.java导致内存泄露的原因很明确:长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周 ...

  4. accept()函数用来告诉Qt,事件处理函数“接收”了这个事件,不要再传递;ignore()函数则告诉Qt,事件处理函数“忽略”了这个事件,需要继续传递(看一下QWidget::mousePressEvent的实现,最为典型。如果希望忽略事件,只要调用父类的响应函数即可)

    QEvent的accept()和ignore()一般不会用到,因为不如直接调用QWidget类的事件处理函数直接,而且作用是一样的,见下面的例子. 推荐直接调用QWidget的事件处理函数.而不是调用 ...

  5. linux CANopenSocket 初试

    /************************************************************************************** * linux CANo ...

  6. VS2013Xml文件节点导航插件开发

    一.功能描述 该插件的功能跟代码文件的导航功能类似,只是下拉框里的内容是元素的某一属性值,如图-1所示 图-1 当点击下拉框的选项后,会自动定位到该内容在xml文件的位置.此功能适用于xml文件内容较 ...

  7. session关联接口

    #coding:utf-8 import requests,json,re ''' session关联接口,第一步访问登录接口,headers中要有cookie,不然会登录失败,登录成功后,添加随笔保 ...

  8. HDU 5912 Fraction (模拟)

    题意:给定一个分式,让你化简. 析:从分母开始模拟分数的算法,最后约分. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...

  9. Linux系统调用之open(), close() (转载)

    转自:http://joe.is-programmer.com/posts/17463.html open函数可以打开或创建一个文件. #include <sys/types.h> #in ...

  10. ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(三)密码修改以及密码重置

     前言: 本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作. 本系列文章主要参考资料: 微软文档:https://docs.microsoft.com/zh-cn/as ...