[ABAP] Einfacher REST-Service (ohne OData)

Piyush Philip auf sap.com hat eine simple Möglichkeit beschrieben, wie man in ABAP einen REST-Service mit den entsprechenden CRUD-Methoden implementieren kann. Dazu ist im Wesentlichen nur eine eigene Handler-Klasse zu implementieren, die von IF_HTTP_EXTENSION erbt. Diese Handler-Klasse wird von einem Service des ICF gerufen.

  1. SE80 -> Neue Klasse anlegen
  2. In der Klasse im Reiter Interfaces: vom Interface IF_HTTP_EXTENSION erben
  3. die Interface-Methode IF_HTTP_EXTENSION~HANDLE_REQUEST implementieren (implementiert nur REST, kein OData-Sevice!)
    METHOD if_http_extension~handle_request.
    
    * Typen
        TYPES: BEGIN OF ty_json_request,
                 param1 TYPE char64,
                 param2 TYPE char64,
               END OF ty_json_request.
    
        TYPES: BEGIN OF ty_json_response,
                 success   TYPE string,
                 msg       TYPE string,
                 operation TYPE string,
               END OF ty_json_response.
    
    * Variablen
        DATA: rv_response TYPE ty_json_response.
        DATA: it_path_info TYPE stringtab.
        DATA: it_query_strings TYPE stringtab.
    
    * Header Fields auslesen
        DATA: it_header_fields TYPE tihttpnvp.
        server->request->get_header_fields( CHANGING fields = it_header_fields ).
    
    * Request Methode (POST, GET, PUT, DELETE ...)
        DATA(lv_request_method) = server->request->get_header_field( name = '~request_method' ).
    
    * URL Pathinfo
        DATA(lv_path_info) = server->request->get_header_field( name = '~path_info' ).
    * erstes '/' entfernen
        SHIFT lv_path_info LEFT BY 1 PLACES.
        SPLIT lv_path_info AT '/' INTO TABLE it_path_info.
    
    * URL Parameter (Query Strings) für Auswertung
        DATA(lv_query_string) = server->request->get_header_field( name = '~query_string' ).
        SPLIT lv_query_string AT '&' INTO TABLE it_query_strings.
    
    * Request Methoden behandeln
        CASE lv_request_method.
    * C (Create)
          WHEN 'POST'.
    * Beispiel: per Request übermittelte JSON-Daten auslesen und behandeln
    * Content-Typ auslesen
            DATA(lv_request_content_type) = server->request->get_content_type( ).
    
    * Wenn Content-Typ JSON
            IF lv_request_content_type CP if_rest_media_type=>gc_appl_json.
    * JSON-Daten auslesen und nach ABAP-Typ konvertieren -> Parameternamen müssen übereinstimmen, sonst HTTP 500!
              DATA(lv_request_cdata) = server->request->get_cdata( ).
    
              DATA: lv_request_abap TYPE ty_json_request.
    
              TRY.
                  /ui2/cl_json=>deserialize( EXPORTING json = lv_request_cdata
                                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                                             CHANGING  data = lv_request_abap ).
    
                CATCH cx_root INTO DATA(e_txt).
                  MESSAGE e_txt->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
              ENDTRY.
    
              rv_response = VALUE #( success   = 'true'
                                     msg       = lv_request_abap-param1 && ' | ' && lv_request_abap-param2
                                     operation = lv_request_method ).
            ELSE.
              rv_response = VALUE #( success   = 'false'
                                     msg       = 'Request content type must be application/json'
                                     operation = lv_request_method ).
            ENDIF.
    * R (Read)
          WHEN 'GET'.
            DATA(lv_param) = server->request->get_form_field( 'param' ).
    
            rv_response = VALUE #( success   = 'true'
                                   msg       = lv_param
                                   operation = lv_request_method ).
    * U (Update)
          WHEN 'PUT'.
            rv_response = VALUE #( success   = 'true'
                                   msg       = ''
                                   operation = lv_request_method ).
    * D (Delete)
          WHEN 'DELETE'.
            rv_response = VALUE #( success   = 'true'
                                   msg       = ''
                                   operation = lv_request_method ).
    
    * unerlaubte Methoden abfangen
          WHEN OTHERS.
    * Fehlercode 405 zurückgeben
            server->response->set_status( code   = cl_rest_status_code=>gc_client_error_meth_not_allwd
                                          reason = 'method not allowed' ).
    
            server->response->set_content_type( if_rest_media_type=>gc_appl_json ).
    
            server->response->set_header_field( name  = if_http_header_fields=>allow
                                                value = 'POST,GET,PUT,DELETE' ).
    
            rv_response = VALUE #( success   = 'false'
                                   msg       = 'error'
                                   operation = lv_request_method ).
        ENDCASE.
    
    * wenn erfolgreich, dann Wert zurückgeben
        IF rv_response-success = 'true'.
    * HTTP-Status 200
          server->response->set_status( code   = cl_rest_status_code=>gc_success_ok
                                        reason = 'Ok.' ).
    
    * application/json
          server->response->set_content_type( if_rest_media_type=>gc_appl_json ).
    
    * Service-Response als JSON zurückgeben
          server->response->set_cdata( data = /ui2/cl_json=>serialize( data        = rv_response
                                                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case ) ).
    
        ENDIF.
    
    ENDMETHOD.
    
  4. SICF -> neue ICF-Node (Sub-Element) unterhalb default_host -> sap -> bc als neuen unabhängigen Service anlegen
  5. in der Handlerliste des Service die erstellte Klasse als “Handler” angeben
  6. auf die neue ICF-Node rechtsklicken -> Service aktivieren
  7. auf die neue ICF-Node rechtsklicken -> Service testen
  8. URL aus Browserfenster kopieren (http://domain.de/sap/bc/servicename?sap-client=900)
  9. im Browser mir einem Rest-Plugin (FireFox-Prugin: Rested) testen

Links

[AJAX] $.get()

Allgemein

$.get(URL, callback); 

Beispiel

// https://www.w3schools.com/jquery/jquery_ajax_get_post.asp
$("btn1").click(function(){
    // url
    $.get("btn1_get.php",
    // callback
    function(data, status){
        console.log("Data: " + data);
        console.log("Status: " + status);
    });
}); 

[MS Access] Binärdaten aus einer Datei lesen

' https://stackoverflow.com/questions/660312/how-can-i-read-a-binary-file-using-vba
Sub BtnReadBinaryClick()
    ' Dateipfad + Dateiname
    Dim sFile As String
    sFile = "c:\\temp\\test.bin"
   
    ' Array für Binärdaten
    Dim byaFileData() As Byte
   
    ' Dateizeiger
    Dim fd As Integer
    ' nächsten freien Dateizeiger holen
    fd = FreeFile

    ' Datei zum binären Lesen öffnen
    Open sFile For Binary Access Read As #fd
    ' Array an Dateigröße anpassen
    ReDim byaFileData(0 To LOF(fd) - 1)
    ' Datei in Array einlesen
    Get #fd, , byaFileData
   
    ' hier byaFileData bearbeiten
    ...
   
    ' Datei schließen
    Close #fd
End Sub

[JavaScript] JSON-Datei per HTTP-Request holen und parsen

// Link zur JSON-Datei
let sURL = 'https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json';
// Request-Object
let request = new XMLHttpRequest();
// MIME-Typen
// txt: "text/plain"
// html: "text/html"
// json: "application/json"
request.overrideMimeType('application/json');
// Requestmethode
request.open('GET', sURL, true);
// Datentyp
request.responseType = 'text';
// Handler für asyncrone Antwort des Requests
// wenn Daten erfolgreich geladen
request.onload = function() {
  console.log('onload()');
  // gesamte Response ausgeben
  let sJSON = request.response;
  console.log(sJSON);
  
  // JSON-Daten parsen
  let oJSON = JSON.parse(sJSON);
  // Wert eines Attributes ausgeben
  console.log(oJSON.homeTown);
};
// bei Änderung des Ready-States
request.onreadystatechange = function () {
  console.log('onreadystatechange()');
  console.log(request.readyState);
  console.log(request.status);
  console.log(request.statusText);
};
// Request absenden
request.send();

[ABAP] Datenausgabe mit cl_demo_output

Demoprogramme

* DEMO_USAGE_OUTPUT_GET
* DEMO_USAGE_OUTPUT_STATIC
* DEMO_USAGE_OUTPUT_INSTANCE
* DEMO_USAGE_OUTPUT_STREAM

Text mit Abschnitten

* Abschnitt 1
cl_demo_output=>begin_section( '1. Überschrift' ).
cl_demo_output=>begin_section( '1.1 Überschrift' ).
* Text
cl_demo_output=>write_text( 'Text 1.1' ).
cl_demo_output=>write( 'Text 1.1 non proportional' ).
cl_demo_output=>end_section( ).
cl_demo_output=>end_section( ).
* Abschnitt 2
cl_demo_output=>begin_section( '2. Überschrift' ).
cl_demo_output=>begin_section( '2.1 Überschrift' ).
* Text
cl_demo_output=>write_text( 'Text 2.1' ).
cl_demo_output=>write( 'Text 2.1 non proportional' ).
cl_demo_output=>end_section( ).
cl_demo_output=>end_section( ).
* alles anzeigen
cl_demo_output=>display( ).

Überschrift

cl_demo_output=>next_section( 'Überschrift' ).

Variablen, Strukturen, Tabellen

TYPES: BEGIN OF ty_struct,
         f1 TYPE string,
         f2 TYPE i,
       END OF ty_struct.

TYPES: ty_it_tab TYPE STANDARD TABLE OF ty_struct WITH DEFAULT KEY.

DATA(lv_struct) = VALUE ty_struct( f1 = 'Field1' f2 = 1 ).
DATA(it_tab) = VALUE ty_it_tab( ( f1 = 'T1' f2 = 1 )
                                ( f1 = 'T2' f2 = 2 )
                                ( f1 = 'T3' f2 = 3 ) ).

cl_demo_output=>write_data( value = -100         name = 'Zahl' ).
cl_demo_output=>write_data( value = 'ein String' name = 'Text' ).
cl_demo_output=>write_data( value = lv_struct    name = 'Struct' ).
cl_demo_output=>write_data( value = it_tab       name = 'Tab' ).
* alles anzeigen
cl_demo_output=>display( ).

Trennlinie

cl_demo_output=>write_text( 'Oben' ).
cl_demo_output=>line( ).
cl_demo_output=>write_text( 'Unten' ).
* alles anzeigen
cl_demo_output=>display( ).

HTML

* HTML hinzufügen
cl_demo_output=>write_html( '<b>Text bold</b>' ).
* HTML anzeigen
cl_demo_output=>display_html( '<i>Text italic</i>' ).

XML

DATA: lv_xml TYPE string.
...
cl_demo_output=>display_xml( lv_xml ).

JSON

DATA: lv_json TYPE xstring.
...
cl_demo_output=>display_json( lv_json ).

Plaintext ausgeben

cl_demo_output=>set_mode( cl_demo_output=>text_mode ).
cl_demo_output=>write_data( value = it_tab name = 'Tab' ).
cl_demo_output=>display( ).

oder

SELECT * FROM tnapr INTO TABLE @DATA(it_tnapr).
cl_demo_output=>new( 'TEXT' )->display( it_tnapr ).

HTML-Code generieren lassen

SELECT * FROM mara INTO TABLE @DATA(it_mara).
IF sy-subrc = 0.
  cl_demo_output=>write_data( it_mara ).
* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING
                                title        = 'Daten aus der Tabelle MARA'
                                html_string  = lv_html
                                container    = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.
ENDIF.

Links