In ABAP Make an API HTTP Request. / ABAP HTTP Request
--
This article is about make an HTTP request inside of an ABAP program.
Use the following code to make the connection and get the response. In Variant 1 the reference to the URL destination in the variable lv_http_dest
has to be set via the transaction code SM59. Here is an example:
In the example of cause please replace the Traget System Settings with the URL you want to make the connection to.
The reference name would then be: “SAP_SCPI_ConEmp” . This we will use for the following code:
DATA: lv_http_dest TYPE rfcdest VALUE 'SAP_SCPI_ConEmp'.
DATA: o_client TYPE REF TO if_http_client.TRY.
cl_http_client=>create_by_destination(
EXPORTING destination = lv_http_dest
IMPORTING client = o_client).
o_client->request->set_method( if_http_request=>co_request_method_post ).
o_client->send( ).
o_client->receive( ).
o_client->response->get_status( IMPORTING
code = lv_http_rc
reason = lv_reason ).
IF lv_http_rc = 200.
DATA(lv_json) = o_client->response->get_cdata( ).
WRITE lv_json.
ELSE.
WRITE: / 'Error: ', lv_http_rc.
ENDIF.
o_client->close( ).
CATCH cx_root INTO DATA(e_txt).
WRITE: / e_txt->get_text( ).
ENDTRY.
The other Method(HACK)!:
DATA(lv_url) = |https://www.google.de/|.
DATA: o_client TYPE REF TO if_http_client.
TRY.
cl_http_client=>create_by_destination(
EXPORTING url = lv_url
IMPORTING client = o_client).
o_client->request->set_method( if_http_request=>co_request_method_post ).
o_client->send( ).
o_client->receive( ).
o_client->response->get_status( IMPORTING
code = lv_http_rc
reason = lv_reason ).
IF lv_http_rc = 200.
DATA(lv_json) = o_client->response->get_cdata( ).
WRITE lv_json.
ELSE.
WRITE: / 'Error: ', lv_http_rc.
ENDIF.
o_client->close( ).
CATCH cx_root INTO DATA(e_txt).
WRITE: / e_txt->get_text( ).
ENDTRY.
Conclusion: I hope it helped. Happy Coding!