cancel
Showing results for 
Search instead for 
Did you mean: 

Issue calling remote odata service (via approuter) from trial version of abap environment on SCP

annkoolen
Explorer
0 Kudos
380

Hi,

I tried to connect to an on prem odata service from my abap environment by means of an approuter deployed on my trial cloud foundry environment.

I keep getting back the error “Server response has the wrong format (content-type) ‘text/html’.”

My code is pretty much exactly the same as the one provided in this blog: https://blogs.sap.com/2019/10/20/how-to-call-a-remote-odata-service-from-the-trial-version-of-sap-cl....

Does my approuter somehow determine the content-type (I used this tutorial https://developers.sap.com/group.cp-connectivity-odata.html, so it should be pretty basic)? And is there a way to fix it in the abap environment or should I be looking to the code of my approuter?

Thanks very much in advance!

CLASS zcl_call_odata_onprem_trial DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .


  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
    DATA projects TYPE STANDARD TABLE OF zprojectset.
  PRIVATE SECTION.


    METHODS get_http_client
      IMPORTING base_url           TYPE string
      RETURNING VALUE(http_client) TYPE REF TO if_web_http_client
      RAISING
                cx_web_http_client_error
                cx_http_dest_provider_error.


    METHODS get_odata_response
      IMPORTING http_client TYPE REF TO if_web_http_client
      EXPORTING entityset   LIKE projects
      RAISING
                cx_web_http_client_error
                /iwbep/cx_gateway.


ENDCLASS.


CLASS zcl_call_odata_onprem_trial IMPLEMENTATION.


  METHOD if_oo_adt_classrun~main.


    DATA base_url TYPE string.
    base_url = 'https://approuter-demo-f0bf5435-fc3b-4c52-a4f0-c1bc240da7c0.cfapps.eu10.hana.ondemand.com/'.
    TRY.
        DATA(http_client) = get_http_client( base_url = base_url ).
      CATCH cx_web_http_client_error cx_http_dest_provider_error INTO DATA(error_creating_http_client).
        out->write( error_creating_http_client->get_longtext(  )  ).
        EXIT.
    ENDTRY.


    TRY.
        get_odata_response(
         EXPORTING http_client = http_client
         IMPORTING entityset = DATA(projects) ).
      CATCH cx_web_http_client_error /iwbep/cx_gateway INTO DATA(error_calling_OData_service).
        "handle exception
        out->write( error_calling_OData_service->get_longtext(  )  ).
        EXIT.
    ENDTRY.


    LOOP AT projects INTO DATA(project).
      out->write( project ).
    ENDLOOP.


  ENDMETHOD.


  METHOD get_http_client.
    http_client = cl_web_http_client_manager=>create_by_http_destination( i_destination =  cl_http_destination_provider=>create_by_url( i_url = base_url ) ).


    http_client->get_http_request( )->set_authorization_basic(
        i_username = #########
        i_password = #########
    ).


  ENDMETHOD.


  METHOD get_odata_response.


    DATA entity LIKE LINE OF entityset.


    DATA(client_proxy) = cl_web_odata_client_factory=>create_v2_remote_proxy(
      EXPORTING
        iv_service_definition_name = 'ZSC_PROJECTSDEMO'
        io_http_client             = http_client
        iv_relative_service_root   = '/sap/opu/odata/sap/ZPPM_FIN_CLOSURE_SRV/' ).


    DATA(odata_request) = client_proxy->create_resource_for_entity_set( 'PROJECTSET' )->create_request_for_read( ).


    odata_request->set_top( 5 )->set_skip( 0 ).


* retrieve business data
    DATA(odata_response) = odata_request->execute( ).
    odata_response->get_business_data( IMPORTING et_business_data = entityset ).


  ENDMETHOD.


ENDCLASS.

Accepted Solutions (0)

Answers (0)