cancel
Showing results for 
Search instead for 
Did you mean: 

CAP - Node js - How to use binary data in deep structures?

robert_quindt
Member
0 Kudos
815

Hi,

I tried to extend the media sample from https://github.com/SAP-samples/cloud-cap-samples to check out how to display binary data in deep structures.

As example I created an entity Parent having a property "media" as an Composition of Media.

I deleted the media-service.js as to handle the calls automaticly by the default mechanism.

Then I added the Parent entity to the service. For test I added a new Parent object, created an "media" object and PUT the "Test.png" as the binary into it.

Extending the media.http I can view the binary when calling .../Media(1)/content. I also checked it in the Browser and can see, that it is served with Content-Type image/png.

When calling .../Parent(1)/media/content I get the object, but with the Content-Type application/octet-stream.

Has someone a solution how to get the object with the saved Content-Type, when reading it from a deep structure? Is that maybe a bug as it depends GET Request and not the saved object?

data-model.cds:

namespace sap.capire.media;
entity Media {
 key id : Integer;
 @Core.MediaType : mediaType
 content : LargeBinary;
 @Core.IsMediaType : true
 mediaType : String;
 fileName : String;
 applicationName : String;
}
entity Parent {
 key id : Integer;
 media : Composition of Media;
}
media-service.cds:
using { sap.capire.media as db } from '../db/data-model';
namespace sap.capire.media;
service MediaServer {
 entity Media as projection on db.Media ;
 entity Parent as projection on db.Parent;
} 
media.http:
### Requires REST Client for VS Code
### https://marketplace.visualstudio.com/items?itemName=humao.rest-client
###
@protocol = http
@host = localhost
@port = 4004
### Read Pictures
GET {{protocol}}://{{host}}:{{port}}/media-server/Media
Authorization: Basic admin:
### Create Picture with mediatype
POST {{protocol}}://{{host}}:{{port}}/media-server/Parent
Authorization: Basic admin:
Accept: application/json
Content-Type: application/json
{ 
 "id": 1,
 "media":{
 "id": 1,
 "mediaType" : "image/jpeg"
 }
}
### Upload Binary PNG
PUT {{protocol}}://{{host}}:{{port}}/media-server/Parent(1)/media/content
Authorization: Basic admin:
Content-Type: image/png
< ./Test.png
### Read Binary
GET {{protocol}}://{{host}}:{{port}}/media-server/Media(1)/content
Authorization: Basic admin:
### Read Binary
GET {{protocol}}://{{host}}:{{port}}/media-server/Parent(1)/media/content
Authorization: Basic admin:
### Delete Image
DELETE {{protocol}}://{{host}}:{{port}}/media-server/Media(1)
Authorization: Basic admin:

best regards

Robert Quindt

Accepted Solutions (1)

Accepted Solutions (1)

vitaly_kozyura
Advisor
Advisor
0 Kudos

Hi Robert,

The issue is fixed. The fix to be provided with the next release of @sap/cds-runtime. You can follow CAP releases at https://cap.cloud.sap/docs/releases/changelog/.

Best regards,

Vitaly

Answers (1)

Answers (1)

johannesvogel
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Robin,

I can reproduce your issue.

We are looking into a fix.

Thanks for reporting!

Johannes