Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to figure out request schema for delete in $metadata?

0 Kudos

Hi, I am working on developing the request and response schema for POST, PUT, DELETE, GET dynamically using the metadata from https://services.odata.org/V4/TripPinService/$metadata.

I am able to build request XSD dynamically for POST and PUT. But I am not able to figure out the request schema for Delete, what fields should be there in the request/URL from metadata. For example: In the above metadata URL there is an entity named as Person. So, is there a tag in Odata standards by which I can recognize that this field in metadata should be used in delete for deleting a person?

Second, how should I figure out the response schema from metadata, or should I consider the response schema the same as the request schema? Thank you, the response would be appreciated on this.

1 ACCEPTED SOLUTION

CarlosRoggan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Vinay,

yes, you're correct.
See the spec: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_CanonicalURL

So if you're generically determining the DELETE URL, then you should use 'username=mia', because the key might be composed by multiple properties

The second question I answered above: the response for DELETE is empty.
This behavior is not defined in the metadata. It is common for HTTP and REST requests. The OData protocol builds upon REST and HTTP, as such you can rely on it.
Furthermore, the behavior is detailed out in the spec (I've given the link above)
In case of DELETE, the response must be always empty, as the entity doesn't exist anymore.
In case of POST the response contains the full created entity, because this can be different from the sending request. But since OData V4, the behavior can be configured, to keep the response empty to make request faster. Same for PUT

Hope this clarifies your question?

BTW, there are libraries for OData, for java, node.js and abap. These libraries do the work for you. Not sure if this can help in your use case

Kind Regards,

Carlos

6 REPLIES 6

former_member34
Product and Topic Expert
Product and Topic Expert
0 Kudos

Thank you for visiting SAP Community to get answers to your questions. Since this is your first question, I recommend that you familiarize yourself with our Q&A Tutorial: https://developers.sap.com/tutorials/community-qa.html, as it provides tips for preparing questions that draw responses from our members. Should you wish, you can revise your question by selecting Actions, then Edit.

By adding a picture to your profile you encourage readers to respond: https://www.youtube.com/watch?v=46bt1juWUUM

Many thanks!

CarlosRoggan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

to delete an entity you just need to fire a DELETE request to the URL of the entity. The URL is the same which you use for READ.
The response is empty, as such the status is 204 No content
The spec can be found here:

http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_DeleteanEntity

You may check the metadata first, if the deletion is allowed at all for that entity.
This is indicated with annotations. Typically, if a service doesn't allow to delete an entity, there's an annotation saying deletable=false, in V4 there are predefined annotation libraries (vocabularies) like DeleteRestrictions, applied to the entity with value false.

hope that helps,

Carlos

0 Kudos

Hi Carlos,

Thanks for your response. I went through the official documentation of OData and already figured out how to delete an entity. For eg:

I have a postman request with DELETE method with URL: http://services.odata.org/V4/TripPinService/People('miathompson')

But the actual question is still there that how will be able to figure out from metadata that this field should be used for deleting an entity. Like in the above URL the miathompson is the value of the field username. It will not work if I put any other field value.

The above screenshot is the metadata for the Person entity type which comes under the EntitySet People. So how will one figure it out by going through metadata that username is the field which will be used while deleting or reading a single entity, which will be used in URL?

Is <PropertyRef> or <Key> is identification in the metadata by which one will know ?

Please refer to the documentation if any.

CarlosRoggan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Vinay,

sorry if I didn't get you correctly. So actually your question is how to point to one specific entry. You have a list of persons (= collection = entity set) and you want to READ ONE specific.
How to address it.
This is done by marking one of the properties as "key" field.
Remember that OData is frequently explained as "SQL for the web"
It works similar.
You see in the metadata that <key> which points to the <property> which must never be "null"

Note that the URL

/V4/TripPinService/People('miathompson')
is an abbreviation of

/V4/TripPinService/People('UserName=miathompson')

abbreviation is possible because there's only one key field.
Your key can also be composed by 2 or more properies, like in SQL.
Then your URL would be ......('prop1=xx, prop2=yy'))

This is how you compose a READ URL and also a PUT and DELETE URL

Working with OData V4 can become complex and you need to have always these 2 specification links at hand:

http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html

and

http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html

(from here: https://www.odata.org/documentation/)

Even the sample service is complex and shows advanced features which not everybody will need

Hope this helps,

Kind Regards,

Carlos

0 Kudos

Hi Carlos, Thank you for your quick response . Correct me if I am wrong

So while constructing the URL for a DELETE or READ request from metadata, every time I should use the field which is defined as <Key> in metadata for an Entity. So whenever I go through metadata and see a field(for eg; username) with <Key> for an Entity type, then I should consider that Key field for deleting or reading an Entity. ?

By just defining the value of the Key field in URL?

/V4/TripPinService/People('miathompson')

and also the second question I asked remains unanswered, could you please help in that too?

Ques: Second, how should I figure out the response schema from metadata, or should I consider the response schema the same as the request schema?

CarlosRoggan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Vinay,

yes, you're correct.
See the spec: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_CanonicalURL

So if you're generically determining the DELETE URL, then you should use 'username=mia', because the key might be composed by multiple properties

The second question I answered above: the response for DELETE is empty.
This behavior is not defined in the metadata. It is common for HTTP and REST requests. The OData protocol builds upon REST and HTTP, as such you can rely on it.
Furthermore, the behavior is detailed out in the spec (I've given the link above)
In case of DELETE, the response must be always empty, as the entity doesn't exist anymore.
In case of POST the response contains the full created entity, because this can be different from the sending request. But since OData V4, the behavior can be configured, to keep the response empty to make request faster. Same for PUT

Hope this clarifies your question?

BTW, there are libraries for OData, for java, node.js and abap. These libraries do the work for you. Not sure if this can help in your use case

Kind Regards,

Carlos