楼主也是第一次接触到OData.最初看到OData 这个词时 ,楼主也是一脸懵逼.
在维基百科 是这样定义OData的.
OData: 英文全名是Open Data Protocol(OData),它是一个开源的协定,以简单和标准的方法,来建造或消除可查询和可操作的RESTful API。它创始于2007年的微软公司.简而言之, OData协议,是以数据模型的操作为核心,通过相关工具,能快速构建出标准化的Restful 的API.
看完这些我估计大家还是糊里糊涂的.其实简单的来说吧 ,就是查询数据一般就用get,删除用delete ,修改用patch , 创建用post.
请求权限 基础权限(Basic Authorization) 即只需要提供用户名和密码
同样对现有数据的修改,PATCH操作和PUT操作不同之处:PUT操作提交的请求数据体,必须包含需要修改的数据和以前没有修改的数据字段,PUT操作会根据新提交的各个字段进行比较,发现有值不同的地方,就会进行更改。如果PATCH, 只需要提供需要修改的字段和对应的新的数值。
在OData 协议提供的典型的查询参数:
$count. 返回查询结果的数量
$expand. 将查询的结果数据模型进行扩展,扩展到相关联的数据模型。
$filter. 查询结果进行过滤
$format. 查询结果设置数据格式:比如xml,json.
$inlinecount.
$orderby. 查询结果按照一定规则排序
$search. 设置全文搜索的条件
$select.
$skip
$skiptoken
$top 获取前面多少条
$filter关键字,可以连接后面的条件表达式,格式为: $filter = <fieldname> <operator> <value>, 比如 $filter= Company eq 'xx有限公司'。
“expand”关键字,表示返回的数据模型除了根节点的数据,还包含了相关关联节点上的数据。
“$search”关键字,可以对数据里面的所有开启了“搜索功能”的字段,进行全文搜索
- get 例子
请求url = https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection?$format=json&$filter=Name eq '测试111111'
{
"d": {
"results": [
{
"__metadata": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')",
"type": "c4codata.LeanLead",
"etag": "W/\"datetimeoffset'2018-01-04T07%3A13%3A45.0486160Z'\""
},
"ETag": "/Date(1515050025048)/",
"ObjectID": "00163E217B181EE7BCA3D9913A1DAFFD",
"GroupCode": "",
"ID": "1695",
"OriginTypeCode": "Z02",
"PriorityCode": "3",
"EndDate": "/Date(1517616000000)/",
"StartDate": "/Date(1515024000000)/",
"QualificationLevelCode": "",
"ResultReasonCode": "",
"ApprovalStatusCode": "",
"ConsistencyStatusCode": "2",
"UserStatusCode": "01",
"SurveyTotalScoreValue": 0,
"CreationDateTime": "/Date(1515050025048)/",
"DistributionChannelCode": "",
"DivisionCode": "",
"SalesGroupID": "",
"SalesGroupUUID": null,
"SalesOfficeID": "",
"SalesOfficeUUID": null,
"SalesOrganisationID": "",
"SalesOrganisationUUID": null,
"SalesTerritoryID": "",
"SalesTerritoryUUID": null,
"SalesTerritoryName": "",
"OwnerPartyIDSales": "",
"OwnerPartyUUIDSales": null,
"OwnerSalesName": "",
"MarketingUnitPartyID": "",
"MarketingUnitPartyUUID": null,
"MarketingUnitName": "",
"AccountPartyID": "000000000000000000000000000000000000000000000000000600000026",
"AccountPartyUUID": "00163E0C-2A6D-1ED6-A0AC-033C2B74944B",
"AccountPartyName": "八佳电器 八佳电气",
"ContactID": "",
"ContactUUID": null,
"ContactName": "",
"OwnerPartyID": "000000000000000000000000000000000000000000000000008000000030",
"OwnerPartyUUID": "00163E0C-2A6D-1EE6-9FC8-8A8E89C47BBA",
"OwnerPartyName": "凯 胡",
"SalesUnitPartyID": "",
"SalesUnitPartyUUID": null,
"SalesUnitName": "",
"ExpectedRevenueAmount": "0.000000",
"ExpectedRevenueCurrencyCode": "",
"Name": "测试111111",
"NameLanguageCode": "ZH",
"ExternalID": "",
"LifeCycleStatusCode": "1",
"SpecialCommitteeID": "000000000000000000000000000000000000000000000000001000000020",
"SpecialCommitteeName": "固定收益类",
"RespEmployeeID": "000000000000000000000000000000000000000000000000008000000338",
"RespEmployeeName": "",
"LeadOfferEmployeeID": "000000000000000000000000000000000000000000000000008000000339",
"LeadOfferEmployeeName": "",
"CreatorName": "曹 为",
"IsConvertoOpp": false,
"OppConversionedName": "",
"OppConversionedID": "",
"Company": "八佳电器",
"ContactFirstName": "",
"ContactLastName": "",
"ContactPhone": "",
"ContactMobile": "",
"ContactEMail": "",
"AccountCity": "洛阳市",
"AccountState": "180",
"AccountCountry": "CN",
"CampaignID": "",
"EmployeeResponsibleUUID": "00163E0C-2A6D-1EE6-9FC8-8A8E89C47BBA",
"CompanySecondName": "八佳电气",
"CompanyThirdName": "",
"CompanyFourthName": "八佳电器 八佳电气-作废",
"OrganisationAccountABCClassificationCode": "A",
"OrganisationAccountIndustrialSectorCode": "",
"OrganisationAccountContactAllowedCode": "",
"ContactMiddleName": "",
"ContactGenderCode": "",
"ContactAcademicTitleCode": "",
"ContactAdditionalAcademicTitleCode": "",
"ContactAllowedCode": "",
"ContactFormOfAddressCode": "",
"ContactNickName": "",
"ContactMaritalStatusCode": "",
"BusinessPartnerRelationshipBusinessPartnerFunctionTypeCode": "",
"BusinessPartnerRelationshipBusinessPartnerFunctionalAreaCode": "",
"BusinessPartnerRelationshipEngagementScoreNumberValue": "0 ",
"BusinessPartnerRelationshipContactVIPReasonCode": "",
"ContactCorrespondenceLanguageCode": "",
"IndividualCustomerFormOfAddressCode": "",
"IndividualCustomerAcademicTitleCode": "",
"IndividualCustomerGivenName": "",
"IndividualCustomerFamilyName": "",
"IndividualCustomerMiddleName": "",
"IndividualCustomerGenderCode": "",
"IndividualCustomerContactAllowedCode": "",
"IndividualCustomerMaritalStatusCode": "",
"IndividualCustomerABCClassificationCode": "",
"IndividualCustomerNationalityCountryCode": "",
"IndividualCustomerBirthDate": null,
"IndividualCustomerOccupationCode": "",
"IndividualCustomerInitialsName": "",
"IndividualCustomerNamePrefixCode": "",
"IndividualCustomerNonVerbalCommunicationLanguageCode": "",
"IndividualCustomerPhone": "",
"IndividualCustomerEMail": "",
"IndividualCustomerMobile": "",
"IndividualCustomerFax": "",
"IndividualCustomerAddressHouseID": "",
"IndividualCustomerAddressStreetName": "",
"IndividualCustomerAddressCity": "",
"IndividualCustomerAddressCountry": "",
"IndividualCustomerAddressState": "",
"IndividualCustomerAddressPostalCode": "",
"IndividualCustomerAddressCountyName": "",
"AccountPostalAddressElementsStreetPostalCode": "",
"AccountPostalAddressElementsCountyName": "",
"AccountPostalAddressElementsStreetPrefix": "",
"AccountPostalAddressElementsAdditionalStreetPrefixName": "",
"AccountPostalAddressElementsStreetName": "",
"AccountPostalAddressElementsStreetSufix": "",
"AccountPostalAddressElementsAdditionalStreetSuffixName": "",
"AccountPostalAddressElementsHouseID": "",
"AccountPostalAddressElementsPOBoxID": "",
"AccountPhone": "",
"AccountEMail": "",
"AccountMobile": "",
"AccountFax": "",
"AccountWebsite": "",
"AccountLatitudeMeasureUnitCode": "",
"AccountLatitudeMeasure": "0.00000000000000",
"AccountLongitudeMeasureUnitCode": "",
"AccountLongitudeMeasure": "0.00000000000000",
"AccountCorrespondenceLanguageCode": "",
"AccountPreferredCommunicationMediumTypeCode": "",
"ContactFunctionalTitleName": "",
"ContactDepartmentName": "",
"ContactEMailUsageDeniedIndicator": "",
"ContactFacsimileFormattedNumberDescription": "",
"ContactBuildingID": "",
"ContactFloorID": "",
"ContactRoomID": "",
"AccountLegalForm": "Z2",
"AccountDUNS": "",
"Note": "",
"AccountNote": "",
"ContactNote": "",
"GroupCodeText": "",
"OriginTypeCodeText": "中介机构渠道",
"PriorityCodeText": "普通",
"QualificationLevelCodeText": "",
"ResultReasonCodeText": "",
"ApprovalStatusCodeText": "",
"ConsistencyStatusCodeText": "不一致",
"UserStatusCodeText": "未完成",
"DistributionChannelCodeText": "",
"DivisionCodeText": "",
"ExpectedRevenueCurrencyCodeText": "",
"NameLanguageCodeText": "中文",
"LifeCycleStatusCodeText": "未处理",
"LeanLeadAttachmentFolder": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadAttachmentFolder"
}
},
"LeanLeadSalesAndMarketingTeam": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadSalesAndMarketingTeam"
}
},
"LeanLeadExternalParty": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadExternalParty"
}
},
"LeanLeadItem": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadItem"
}
},
"LeanLeadContact": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadContact"
}
},
"LeanLeadBusinessTransactionDocumentReference": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection('00163E217B181EE7BCA3D9913A1DAFFD')/LeanLeadBusinessTransactionDocumentReference"
}
}
}
]
}
}
可以对 LeanLeadSalesAndMarketingTeam, LeanLeadContact等子节点展开扩展
例如 https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadCollection?$format=json&$filter=Name eq '测试111111'&$expand=LeanLeadSalesAndMarketingTeam
"LeanLeadSalesAndMarketingTeam": [
{
"__metadata": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadSalesAndMarketingTeamCollection('00163E217B181EE7BCA3D9913A1D8FFD')",
"type": "c4codata.LeanLeadSalesAndMarketingTeam",
"etag": "W/\"datetimeoffset'2018-01-04T07%3A13%3A45.0486160Z'\""
},
"ETag": "/Date(1515050025048)/",
"ObjectID": "00163E217B181EE7BCA3D9913A1D8FFD",
"ParentObjectID": "00163E217B181EE7BCA3D9913A1DAFFD",
"MainIndicator": true,
"PartyID": "8000000030",
"PartyUUID": "00163E0C-2A6D-1EE6-9FC8-8A8E89C47BBA",
"RoleCode": "39",
"PartyName": "凯 胡",
"Phone": "",
"EMail": "",
"RoleCodeText": "承揽负责人",
"LeanLead": {
"__deferred": {
"uri": "https://my500030.c4c.saphybriscloud.cn/sap/c4c/odata/v1/c4codata/LeanLeadSalesAndMarketingTeamCollection('00163E217B181EE7BCA3D9913A1D8FFD')/LeanLead"
}
}
}
]
2
OData数据的编辑、新建和删除操作,是“POST/PUT/PATCH/DELETE”的请求方式,权限校验方式,除了需要用户名和密码,还需要x-csrf-token.
先get 请求 请求时设置请求的Header, 包含这样的参数:x-csrf-token:fetch
当返回的状态编码为200时,表示请求成功,且x-csrf-token的值
post put patch delete 等操作都要先去get 获取到x-csrf-token
获取x-csrf-token
把获取到Token 放header里面
请求body 的设置 选择raw 模式 和 json类型
其他的 delete put 都类似.
PS:
(
如果使用AFNetWorking请求的话 ,
注意使用 AFHTTPSessionManager* man=[AFURLSessionManager manager];
man.requestSerializer=[AFJSONRequestSerializer serializer];
)