2

I want to set additional header fields for a given destination. Default call works.

example:

Destination destination = DestinationAccessor.getDestination("xyz");
Data data = new DefaultXyzService()
   .withServicePath("/sap/opu/odata/sap/Z_XYZ_SRV")
   .getDataByKey(id)
   .execute(destination);

But how can i set a custom header field? Something like that

List<Header> headers = new ArrayList<Header>();
final Header HEADER = new Header("X-XYZ-ENDUSERNAME", id);
headers.add(HEADER);
DefaultHttpDestination destination2 = new DefaultHttpDestination(destination.asHttp(), headers);
Data data = new DefaultXyzService()
   .withServicePath("/sap/opu/odata/sap/Z_XYZ_SRV")
   .getDataByKey(id)
   .execute(destination2);

results in an error message: "Unable to fetch the metadata : Error fetching the metadata"
Using version 3.1.0.

Thanks and best regards,
Volker

Volker
  • 53
  • 4
  • Is the following operation a suitable option for you...? `.getDataByKey(id).withHeader(String,String).execute(destination.asHttp())` – Alexander Dümont Dec 05 '19 at 11:55
  • 2
    That is part of the solution! After reading your comment i saw that the returning object of withHeader has a method onRequestOnly. `.getDataByKey(id).withHeader(HEADER_ENDUSERNAME,id).onRequestOnly().execute(destination.asHttp());` Thanks a lot. – Volker Dec 05 '19 at 12:35

1 Answers1

1

API consumers of the SAP Cloud SDK are recommended to use the "withHeader" method to define individual HTTP request headers for their OData query:

new DefaultXyzService()
   .withServicePath("/sap/opu/odata/sap/Z_XYZ_SRV")
   .getDataByKey(id)

   // only on entity request
   .withHeader("foo", "bar").onRequestOnly()

   // on all requests, including $metadata lookup
   .withHeader("fizz", "buzz").onRequestAndImplicitRequests()

   .execute(destiantion.asHttp());
Alexander Dümont
  • 903
  • 1
  • 5
  • 9