cancel
Showing results for 
Search instead for 
Did you mean: 

Attachment Service API_CV_ATTACHMENT_SRV does not work from ABAP BTP to On-Premise

Jaman
Participant
0 Kudos

Hello Experts,

I am using this api https://api.sap.com/api/OP_API_CV_ATTACHMENT_SRV_0001/overview to attach the pdf file to the Invoice document.

I am developing this code in ABAP BTP Environment and the attachment of file should happen in on-premise system.

This is how my code looks like,

TRY.

" Create http client

DATA(lo_http_destination) = cl_http_destination_provider=>create_by_comm_arrangement(

comm_scenario = 'Z_INV_ATTACH_API'

comm_system_id = 'Z_INV_ATTACH_API'

service_id = 'Z_API_ATTACH_READ_REST' )

.

DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ).

"adding headers with API Key for API Sandbox

DATA(lo_web_http_request) = lo_http_client->get_http_request( ).

lo_web_http_request->set_header_fields( VALUE #(

( name = 'APIKey' value = 'ArJtd3AEMiU9rpFGREJMUXGy5vA8qtF3' )

( name = 'DataServiceVersion' value = '2.0' )

( name = 'Accept' value = 'application/json' )

( name = 'Slug' value = iv_filename )

( name = 'LinkedSapObjectKey' value = iv_ObjKey )

( name = 'BusinessObjectTypeName' value = iv_botyp )

( name = 'Content' value = iv_string )

) ).

lo_client_proxy = cl_web_odata_client_factory=>create_v2_remote_proxy(

EXPORTING

iv_service_definition_name = 'ZSC_INV_ATTACH_NEW'

io_http_client = lo_http_client

iv_relative_service_root = 'sap/opu/odata/SAP/API_CV_ATTACHMENT_SRV' ).

" Navigate to the resource and create a request for the create operation

lo_request = lo_client_proxy->create_resource_for_entity_set( 'ATTACHMENTCONTENTSET' )->create_request_for_create( ).

" Set the business data for the created entity

lo_request->set_business_data( is_business_data = ls_business_data ).

" Execute the request

lo_response = lo_request->execute( ).

While executing the request, I am getting an exception at below point,(Refer the screenshot)

the importing parameter of this menthod Is_lib_entity_metadata has value as 'http:host:port/sap/opu/odata/SAP/API_CV_ATTACHMENT_SRV/AttachmentContentSet(DocumentInfoRecordDocType='GOS',DocumentInfoRecordDocNumber='EXT000000000001',)

Basically the entity 'AttachmentContentSet' is only for posting. For reading, we have 'GetAllOriginals'.But seems like it's reading with 'AttachmentContentSet'

At end of the call, the file is getting attached, but it's corrupted. Do i miss something here

Can Someone help fixing this error?

Thanks,

Ahamed.

Accepted Solutions (1)

Accepted Solutions (1)

Jaman
Participant
0 Kudos

We can't use Service Consumption Model for achieving the same. Here is the code for the same.

TRY.

DATA http_header_fields TYPE if_web_http_request=>name_value_pairs .

http_header_fields = VALUE #( ( name = if_web_http_header=>accept value = |application/json| )

( name = if_web_http_header=>content_type value = |application/json| )

( name = 'x-csrf-token' value = 'fetch' ) ).

DATA(service_relative_url) = '/sap/opu/odata/SAP/API_CV_ATTACHMENT_SRV/'.

DATA(sap_client) = '300'.

DATA(destination_name_in_dest_srv) = 'APIPORTAL-INV_ATTACH'.

"Destination Required for Basic Authentication. Credentials are stored in the SAP BTP destination service

TRY.

DATA(lo_http_destination) = cl_http_destination_provider=>create_by_cloud_destination(

i_name = destination_name_in_dest_srv

i_authn_mode = if_a4c_cp_service=>service_specific

).

CATCH cx_http_dest_provider_error INTO DATA(lx_http_dest_provider_error).

lx_http_dest_provider_error->get_text(

RECEIVING

result = DATA(lv_msg)

).

"handle exception

ENDTRY.

DATA(relative_url) = |{ service_relative_url }?sap-client={ sap_client }|.

TRY.

DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ).

CATCH cx_web_http_client_error INTO DATA(lx_web_http_client_error).

lx_web_http_client_error->get_text(

RECEIVING

result = DATA(lv_msg1)

).

"handle exception

ENDTRY.

"remove any white spaces since the method set_uri_path does not check for white spaces

CONDENSE relative_url NO-GAPS.

" GET CSRF token

http_client->get_http_request( )->set_uri_path( i_uri_path = relative_url ).

http_client->get_http_request( )->set_header_fields( http_header_fields ).

DATA(http_response) = http_client->execute( if_web_http_client=>get ). "--> works

DATA(http_status_code) = http_response->get_status( ).

DATA(x_csrf_token) = http_response->get_header_field( 'x-csrf-token' ).

DATA(http_response_body) = http_response->get_text( ).

DATA(lv_result) = reverse( substring( val = reverse( iv_filename ) len = 3 ) ).

DATA lv_content_type TYPE string.

IF lv_result = 'pdf'.

lv_content_type = 'application/pdf'.

ELSEIF lv_result = 'txt'.

lv_content_type = 'text/plain'.

ENDIF.

http_client->get_http_request( )->set_uri_path( i_uri_path = '/sap/opu/odata/SAP/API_CV_ATTACHMENT_SRV/AttachmentContentSet' ).

http_header_fields = VALUE #( ( name = if_web_http_header=>accept value = |application/json| )

( name = 'x-csrf-token' value = x_csrf_token )

( name = 'Slug' value = iv_filename )

( name = 'LinkedSapObjectKey' value = iv_objkey )

( name = 'BusinessObjectTypeName' value = iv_botyp )

( name = 'Content-Type' value = lv_content_type )

).

http_client->get_http_request( )->set_header_fields( http_header_fields ).

IF lv_result EQ 'txt'.

http_client->get_http_request( )->set_text(

EXPORTING

i_text = iv_string

).

ELSEIF lv_result EQ 'pdf'.

cl_web_http_utility=>decode_x_base64(

EXPORTING

encoded = iv_string

RECEIVING

decoded = DATA(lv_decodedx)

).

http_client->get_http_request( )->set_binary(

EXPORTING

i_data = lv_decodedx

).

ENDIF.

TRY.

http_response = http_client->execute( if_web_http_client=>post ).

CATCH cx_web_http_client_error INTO DATA(lx_web_http_client_post_error).

lx_web_http_client_post_error->get_text(

RECEIVING

result = DATA(lv_error)

).

ENDTRY.

http_status_code = http_response->get_status( ).

IF http_status_code-code NE '201'.

e_outdata = http_status_code-reason.

ENDIF.

ENDTRY.

Answers (0)