Web Service Restful API

Getting started ... Alles für einen gelungenen Start.
5 Beiträge Seite 1 von 1
5 Beiträge Seite 1 von 1

Web Service Restful API

Beitrag von tekko (ForumUser / 4 / 3 / 0 ) » 18. Okt 2019 16:06

Hallo Leute,
ich muss einen Web Service konsumieren, und habe einen Quellcode bereits für Java.
Das ganze läuft über eine Restful API.

Ich bin völlig neu in ABAP und weiß nicht wie ich den Java Code in Abap umformen soll, oder ob das überhaupt funktioniert.

Code: Alles auswählen.


String s= "Hello World";


URI uri = URI.create("http://api.example.com/");
HttpRequest request = HttpRequest.newBuilder(uri)
    .header("Accept", "application/pdf") 
    .POST(BodyPublishers.ofString(s))
    .build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse< byte[] > response = client.send(request,BodyHandlers.ofByteArray());
byte[] body = response.body();

if (response.statusCode() == 200) {
    File file = new File("label.pdf"); 
    Files.write(file.toPath(), body);
} 


Wäre es im Prinzip möglich 1 zu 1 das in Abap umzuformen, oder muss ich dafür etwas spezielles tun? Sprich mit Export und Import?

Wie gesagt ich bin Neuling in Bereich ABAP und wäre für jede Hilfe dankbar


Re: Web Service Restful API

Beitrag von Tron (Top Expert / 1088 / 17 / 236 ) » 19. Okt 2019 18:13

Moin.

Hier mal 2 Beispiele wie Webservices gehostet (SAP = Server) und wie man Webservices aufruft (SAP = Client).

SAP = Server
einen Test Webservice siehe:
viewtopic.php?f=1&t=17609#p60010
Einfach den Inhalt des Zips mal nach programmieren.

Ansonsten kann man so einen Webservice auch einfach über eine BSP-Applikation verwirklichen.
1.) BSP Applikation anlegen und den Inhalt (bis auf erste Zeile) der Startseite löschen.
000.png
2.) Event OnRequest ausprogrammieren
002.png
3.) Ergebnis
003.png
4.) Ein bischen Code zum Testen !!!
Das PDF ist im Mime Repository abgelegt (ggf. die Mime -Url anpassen)
(Method OnRequest)

Code: Alles auswählen.

* the handler is called whenever a request is made for a particular page
* it is used to restore the internal data structures from the request
DATA ireq TYPE REF TO if_http_request.
DATA ires TYPE REF TO if_http_response.
DATA result TYPE string.
DATA l_mime TYPE string.

ireq = page->get_request( ).
ires = page->get_response( ).

* ? Is it http - POST
DATA l_method TYPE string.
l_method = ireq->get_header_field( '~request_method' ).
IF NOT l_method = 'POST'.
  "  EXIT.
ENDIF.


* Read all Header fields
DATA headerfields TYPE tihttpnvp.
CALL METHOD ireq->get_header_fields
  CHANGING
    fields = headerfields.

* Get response content

DATA lo_mr_api TYPE REF TO if_mr_api.
DATA: l_folder TYPE boole_d,
      l_mimetype TYPE sdokfilaci-mimetype.
DATA l_xstring TYPE xstring.

* instantiate MIME API class
CALL METHOD cl_mime_repository_api=>if_mr_api~get_api
  RECEIVING
    r_mr_api = lo_mr_api.

* Get PDF file from MIME repository
CALL METHOD lo_mr_api->get
  EXPORTING
    i_url              = '/SAP/Zhello_world/How-to_ADS.pdf'
  IMPORTING
    e_is_folder        = l_folder
    e_content          = l_xstring
    e_mime_type        = l_mimetype
  EXCEPTIONS
    parameter_missing  = 1
    error_occured      = 2
    not_found          = 3
    permission_failure = 4
    OTHERS             = 5.


IF sy-subrc = 0.

  l_mime = l_mimetype. " = application/pdf
  ires->set_header_field( name = 'content-type' value =  l_mime ).

* Send result Body content
  ires->set_data( l_xstring ).

  ires->set_status( code = 200 reason = 'OK' ).

ELSE.
  ires->set_status( code = 400 reason = 'hier stimmt was nicht' ).
ENDIF.
..
SAP = Client
..

Code: Alles auswählen.

*&---------------------------------------------------------------------*
*& Report  YBC_HTTP_CLIENT_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ybc_http_client_test.

* data declarations
DATA: client TYPE REF TO if_http_client.


DATA:
host TYPE string VALUE 'http://xamp:8081',
errortext TYPE string.


DATA:
uri TYPE string,
subrc TYPE i,
version TYPE i,
request TYPE REF TO if_http_request.
DATA res_data TYPE xstring.
DATA res_cdata TYPE string.

DATA:buffer TYPE xstring,
     conv TYPE REF TO cl_abap_conv_in_ce,
     buf TYPE TABLE OF tab512 WITH HEADER LINE,
     bytes_read TYPE i,
     fname TYPE string VALUE 'c:\temp\000.html'.


DEFINE m_err_exit.
  if sy-subrc <> 0.
    call method client->get_last_error
      importing
        code    = subrc
        message = errortext.

    exit.
  endif.

END-OF-DEFINITION.


CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = host
  IMPORTING
    client             = client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.

m_err_exit.

client->request->set_header_field( name = '~request_method'
value = 'POST' ).

CALL METHOD client->request->set_form_field
  EXPORTING
    name  = '$get'
    value = 'checked'.



CALL METHOD client->send
*exporting timeout = timeout
  EXCEPTIONS http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3
    OTHERS = 4.

m_err_exit.

CALL METHOD client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.

m_err_exit.


*CALL METHOD client->listen
*  RECEIVING
*    client                     = client
*  EXCEPTIONS
*    http_communication_failure = 1
*    http_no_open_connection    = 2
*    others                     = 3.


*IF sy-subrc <> 0.
*  CALL METHOD client->get_last_error
*    IMPORTING
*      code    = subrc
*      message = errortext.
*
*  EXIT.
*ENDIF.


*CALL METHOD client->response->get_data
*  RECEIVING
*    data = res_data.

CALL METHOD client->response->get_cdata
  RECEIVING
    data = res_cdata.

m_err_exit.


CALL METHOD client->close
  EXCEPTIONS
    http_invalid_state = 1
    OTHERS             = 2.

m_err_exit.

gruß Jens

Folgende Benutzer bedankten sich beim Autor Tron für den Beitrag (Insgesamt 2):
Somani (21. Okt 2019 09:40) • tekko (23. Okt 2019 12:24)

<:: XING-Gruppe Tricktresor::>
Die deutsche Rechtschreibung ist Freeware, du darfst sie kostenlos nutzen –
Aber sie ist nicht Open Source, d. h. du darfst sie nicht verändern oder in veränderter Form veröffentlichen.

Re: Web Service Restful API

Beitrag von tekko (ForumUser / 4 / 3 / 0 ) » 23. Okt 2019 13:12

Tron hat geschrieben:
19. Okt 2019 18:13
Moin.

Hier mal 2 Beispiele wie Webservices gehostet (SAP = Server) und wie man Webservices aufruft (SAP = Client).

SAP = Server
einen Test Webservice siehe:
viewtopic.php?f=1&t=17609#p60010
Einfach den Inhalt des Zips mal nach programmieren.

Ansonsten kann man so einen Webservice auch einfach über eine BSP-Applikation verwirklichen.
1.) BSP Applikation anlegen und den Inhalt (bis auf erste Zeile) der Startseite löschen.
000.png
2.) Event OnRequest ausprogrammieren
002.png
3.) Ergebnis
003.png

4.) Ein bischen Code zum Testen !!!
Das PDF ist im Mime Repository abgelegt (ggf. die Mime -Url anpassen)
(Method OnRequest)

Code: Alles auswählen.

* the handler is called whenever a request is made for a particular page
* it is used to restore the internal data structures from the request
DATA ireq TYPE REF TO if_http_request.
DATA ires TYPE REF TO if_http_response.
DATA result TYPE string.
DATA l_mime TYPE string.

ireq = page->get_request( ).
ires = page->get_response( ).

* ? Is it http - POST
DATA l_method TYPE string.
l_method = ireq->get_header_field( '~request_method' ).
IF NOT l_method = 'POST'.
  "  EXIT.
ENDIF.


* Read all Header fields
DATA headerfields TYPE tihttpnvp.
CALL METHOD ireq->get_header_fields
  CHANGING
    fields = headerfields.

* Get response content

DATA lo_mr_api TYPE REF TO if_mr_api.
DATA: l_folder TYPE boole_d,
      l_mimetype TYPE sdokfilaci-mimetype.
DATA l_xstring TYPE xstring.

* instantiate MIME API class
CALL METHOD cl_mime_repository_api=>if_mr_api~get_api
  RECEIVING
    r_mr_api = lo_mr_api.

* Get PDF file from MIME repository
CALL METHOD lo_mr_api->get
  EXPORTING
    i_url              = '/SAP/Zhello_world/How-to_ADS.pdf'
  IMPORTING
    e_is_folder        = l_folder
    e_content          = l_xstring
    e_mime_type        = l_mimetype
  EXCEPTIONS
    parameter_missing  = 1
    error_occured      = 2
    not_found          = 3
    permission_failure = 4
    OTHERS             = 5.


IF sy-subrc = 0.

  l_mime = l_mimetype. " = application/pdf
  ires->set_header_field( name = 'content-type' value =  l_mime ).

* Send result Body content
  ires->set_data( l_xstring ).

  ires->set_status( code = 200 reason = 'OK' ).

ELSE.
  ires->set_status( code = 400 reason = 'hier stimmt was nicht' ).
ENDIF.
..
SAP = Client
..

Code: Alles auswählen.

*&---------------------------------------------------------------------*
*& Report  YBC_HTTP_CLIENT_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ybc_http_client_test.

* data declarations
DATA: client TYPE REF TO if_http_client.


DATA:
host TYPE string VALUE 'http://xamp:8081',
errortext TYPE string.


DATA:
uri TYPE string,
subrc TYPE i,
version TYPE i,
request TYPE REF TO if_http_request.
DATA res_data TYPE xstring.
DATA res_cdata TYPE string.

DATA:buffer TYPE xstring,
     conv TYPE REF TO cl_abap_conv_in_ce,
     buf TYPE TABLE OF tab512 WITH HEADER LINE,
     bytes_read TYPE i,
     fname TYPE string VALUE 'c:\temp\000.html'.


DEFINE m_err_exit.
  if sy-subrc <> 0.
    call method client->get_last_error
      importing
        code    = subrc
        message = errortext.

    exit.
  endif.

END-OF-DEFINITION.


CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = host
  IMPORTING
    client             = client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.

m_err_exit.

client->request->set_header_field( name = '~request_method'
value = 'POST' ).

CALL METHOD client->request->set_form_field
  EXPORTING
    name  = '$get'
    value = 'checked'.



CALL METHOD client->send
*exporting timeout = timeout
  EXCEPTIONS http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3
    OTHERS = 4.

m_err_exit.

CALL METHOD client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.

m_err_exit.


*CALL METHOD client->listen
*  RECEIVING
*    client                     = client
*  EXCEPTIONS
*    http_communication_failure = 1
*    http_no_open_connection    = 2
*    others                     = 3.


*IF sy-subrc <> 0.
*  CALL METHOD client->get_last_error
*    IMPORTING
*      code    = subrc
*      message = errortext.
*
*  EXIT.
*ENDIF.


*CALL METHOD client->response->get_data
*  RECEIVING
*    data = res_data.

CALL METHOD client->response->get_cdata
  RECEIVING
    data = res_cdata.

m_err_exit.


CALL METHOD client->close
  EXCEPTIONS
    http_invalid_state = 1
    OTHERS             = 2.

m_err_exit.

gruß Jens






Code: Alles auswählen.


REPORT z_webservice.

DATA: client TYPE REF TO if_http_client.


DATA: host      TYPE string VALUE 'http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/^xa^cfa,50^fo100,100^fdHelloWorld^fs^xz',
      errortext TYPE string.

DATA: uri     TYPE string,
      subrc   TYPE i,
      version TYPE i,
      request TYPE REF TO if_http_request.
DATA: res_data TYPE xstring.
DATA: res_cdata TYPE string.

DATA: buffer     TYPE xstring,
      conv       TYPE REF TO cl_abap_conv_in_ce,
      buf        TYPE TABLE OF tab512 WITH HEADER LINE,
      bytes_read TYPE i,
      fname      TYPE string VALUE 'C:\Users\a.offerm\Desktop\LabelSAP\a.pdf'.

DEFINE m_err_exit.
  if sy-subrc <> 0.
    call method client->get_last_error
      importing
        code    = subrc
        message = errortext.

    exit.
  endif.
END-OF-DEFINITION.

CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = host
  IMPORTING
    client             = client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.



m_err_exit.

*client->request->set_header_field( name = '~request_method'
*value = 'POST' ).

client->request->set_header_field( name = '~request_method'
value = 'POST' ).

CALL METHOD client->request->set_form_field
  EXPORTING
    name  = 'Accept'
    value = 'application/pdf'.

CALL METHOD client->send
*exporting timeout = timeout
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.

m_err_exit.



CALL METHOD client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.



m_err_exit.


*CALL METHOD client->listen
*  RECEIVING
*    client                     = client
*  EXCEPTIONS
*    http_communication_failure = 1
*    http_no_open_connection    = 2
*    OTHERS                     = 3.


*IF sy-subrc <> 0.
*  CALL METHOD client->get_last_error
*    IMPORTING
*      code    = subrc
*      message = errortext.
*
*  EXIT.
*ENDIF.


*CALL METHOD client->response->get_data
*  RECEIVING
*    data = res_data.

CALL METHOD client->response->get_cdata
  RECEIVING
    data = res_cdata.

m_err_exit.


CALL METHOD client->close
  EXCEPTIONS
    http_invalid_state = 1
    OTHERS             = 2.

m_err_exit.

ich habe deinen Code jetzt meinen Angaben angepasst. Leider bekomme ich die Fehlermeldung --> http_communication_failure = 1
bei der Methode client->receive.


Woran liegt das ?
Vielen Dank im Vorraus

Re: Web Service Restful API

Beitrag von Tron (Top Expert / 1088 / 17 / 236 ) » 23. Okt 2019 17:05

Moin.
So funktioniert es bei mir.
Siehe Anhang.
in der Variablen "errortext" ist der Fehler beschrieben.
Kann ich nur vermuten, vielleicht darf das SAP nicht ins Internet ?
Mal im Debugger nachsehen.

gruß Jens

Code: Alles auswählen.

*&---------------------------------------------------------------------*
*& Report  Z_WEBSERVICE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  z_webservice.

DATA: client TYPE REF TO if_http_client.


DATA: host      TYPE string VALUE 'http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/^xa^cfa,50^fo100,100^fdHelloWorld^fs^xz',
      errortext TYPE string.

DATA: uri     TYPE string,
      subrc   TYPE i,
      version TYPE i,
      request TYPE REF TO if_http_request.
DATA: res_data TYPE xstring.
DATA: res_cdata TYPE string.

DATA: buffer     TYPE xstring,
      conv       TYPE REF TO cl_abap_conv_in_ce,
      buf        TYPE TABLE OF tab512 WITH HEADER LINE,
      bytes_read TYPE i,
      "fname      TYPE string VALUE 'C:\Users\a.offerm\Desktop\LabelSAP\a.pdf'.
      fname      TYPE string VALUE 'C:\temp\data.png'.

DEFINE m_err_exit.
  if sy-subrc <> 0.
    call method client->get_last_error
      importing
        code    = subrc
        message = errortext.

    exit.
  endif.
END-OF-DEFINITION.

CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = host
  IMPORTING
    client             = client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.

m_err_exit.

client->request->set_header_field( name = '~request_method'
value = 'GET' ).

CALL METHOD client->send
*exporting timeout = timeout
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.

m_err_exit.



CALL METHOD client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4.



m_err_exit.

DATA: i TYPE i,
lt_bin_tab       TYPE solix_tab.


CALL METHOD client->response->get_data
  RECEIVING
    data = res_data.



m_err_exit.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer                = res_data
*   APPEND_TO_TABLE       = ' '
  IMPORTING
    OUTPUT_LENGTH         = i
  TABLES
    binary_tab            = lt_bin_tab
          .


CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    bin_filesize            = i
    filename                = fname
    filetype                = 'BIN'
  CHANGING
    data_tab                = lt_bin_tab
  EXCEPTIONS
    file_write_error        = 1
    no_batch                = 2
    gui_refuse_filetransfer = 3
    invalid_type            = 4
    no_authority            = 5
    unknown_error           = 6
    header_not_allowed      = 7
    separator_not_allowed   = 8
    filesize_not_allowed    = 9
    header_too_long         = 10
    dp_error_create         = 11
    dp_error_send           = 12
    dp_error_write          = 13
    unknown_dp_error        = 14
    access_denied           = 15
    dp_out_of_memory        = 16
    disk_full               = 17
    dp_timeout              = 18
    file_not_found          = 19
    dataprovider_exception  = 20
    control_flush_error     = 21
    not_supported_by_gui    = 22
    error_no_gui            = 23
    OTHERS                  = 24.

IF sy-subrc <> 0.
ENDIF.



CALL METHOD client->close
  EXCEPTIONS
    http_invalid_state = 1
    OTHERS             = 2.

m_err_exit.

Folgende Benutzer bedankten sich beim Autor Tron für den Beitrag:
tekko (24. Okt 2019 09:24)

<:: XING-Gruppe Tricktresor::>
Die deutsche Rechtschreibung ist Freeware, du darfst sie kostenlos nutzen –
Aber sie ist nicht Open Source, d. h. du darfst sie nicht verändern oder in veränderter Form veröffentlichen.

Re: Web Service Restful API

Beitrag von tekko (ForumUser / 4 / 3 / 0 ) » 24. Okt 2019 09:26

Vielen Dank @Tron.
Ich werde es versuchen. Du hast mir sehr viel geholfen.

Seite 1 von 1

Aktuelle Forenbeiträge

AUSpacken einer Lieferung - WS_DELIVERY_UPDATE gelöst
vor 23 Minuten von MICBV 9 / 3974
SELECT + LOOP: Geschwindigkeit?
vor 29 Minuten von foxtrot 11 / 144
STMS via Coding aktualisieren gelöst
vor einer Stunde von ichse18577 7 / 119
ILM SAP HCM - Löschen von Daten
vor 2 Stunden von SaskuAc 4 / 76

Unbeantwortete Forenbeiträge

Report-Entwurf mit Mediator-Entwurfsmuster
vor 2 Tagen von Maximus 1 / 77
Verpacken vorhandener HUs an Auslieferungen
vor 5 Tagen von Trilla_CB 1 / 17
RFC_READ_TABLE mit RANGES selection
vor 6 Tagen von Tron 1 / 88