Ideal Scripts

Embed Size (px)

Citation preview

  • 8/17/2019 Ideal Scripts

    1/2

    * You can directly use the REST service URL as wellcl_http_client=>create_by_destination(EXPORTINGdestination = 'VENDAVO' " Logical destination (specified i

    n function call)IMPORTINGclient = lo_http_client " HTTP Client Abstraction

    EXCEPTIONSargument_not_found = 1destination_not_found = 2destination_no_authority = 3plugin_not_active = 4

    io_http_client = lo_http_client.* Set HTTP version

    lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_ 0 ).

    IF lo_http_client IS BOUND AND lo_rest_client IS BOUND.DATA(id) = 'XYZ'.CONCATENATE 'agreements/' id INTO lv_url.

    HTTP basic authenication* lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.* DATA l_username TYPE string.* DATA l_password TYPE string.

    * l_username = 'user'.* l_password = 'password'.* CALL METHOD lo_http_client->authenticate* EXPORTING* username = l_username* password = l_password.* Create REST client instance

    CREATE OBJECT lo_rest_client* Set the URI if any

    cl_http_utility=>set_request_uri(EXPORTINGrequest = lo_http_client->request " HTTP Framework (iHTTP) HTTP Requ

    est

    uri = lv_url " URI String (in the Form of /path?query-string)* If you are using cl_http_client=>create_by_url use this code to supress and pass your* HTTP basic authenication* lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.* DATA l_username TYPE string.* DATA l_password TYPE string.* l_username = 'user'.* l_password = 'password'.* CALL METHOD lo_http_client->authenticate* EXPORTING* username = l_username

    * password = l_password.* Create REST client instance

    CREATE OBJECT lo_rest_clientEXPORTING

    ). "Create a structure(or deep) that exactly matches your JSON response

    abap_response TYPE zca_serno_ln,lo_response TYPE REF TO if_rest_entity.

    * Create HTTP intance using RFC restination created

  • 8/17/2019 Ideal Scripts

    2/2

    "Data:DecalaraitonDATA: ,

    lo_rest_client TYPE REF TO cl_rest_http_client,lv_url TYPE string,lo_http_client TYPE REF TO if_http_client

      lv_body TYPE string,token TYPE string,agreements TYPE string,

    * You can directly use the REST service URL as wellcl_http_client=>create_by_destination(EXPORTINGdestination = 'VENDAVO' " Logical destination (specified i

    n function call)IMPORTINGclient = lo_http_client " HTTP Client Abstraction

    EXCEPTIONSargument_not_found = 1destination_not_found = 2destination_no_authority = 3plugin_not_active = 4internal_error = 5

    OTHERS = 6).

    * Set request header if anyCALL METHOD lo_rest_client->if_rest_client~set_request_header

    EXPORTINGiv_name = 'auth-token'iv_value = token.

    * HTTP GETlo_rest_client->if_rest_client~get( ).

    * HTTP responselo_response = lo_rest_client->if_rest_client~get_response_entity( ).

    * HTTP return statusDATA(http_status) = lo_response->get_header_field( '~status_code' ).

    * HTTP JSON return stringDATA(json_response) = lo_response->get_string_data( ).