This is a test message to test the length of the message box.
Login
|
ABAP Quick POST Request
Created by Software-Heroes

ABAP Quick - Create POST Request

22048

How can you easily create a POST request on a domain and process the result? We'll show you how the REST connection works very easily.

Advertising


The execution of POST requests is normally not required when working in a SAP system, since numerous other and more secure options are available for interfaces. The following code should serve you only for practicing and trying out new possible ways to work.

 

Core classes

For the core of the call we use the following three standard classes of SAP. With these you can make the most important calls and accept the result of the request again:

  • CL_HTTP_CLIENT
  • CL_HTTP_REQUEST
  • CL_HTTP_RESPONSE

 

Execution

For the request, you first need the client to whom we already pass the call URL. By inline declaration, we can create the client directly in the report upon return.

CONSTANTS c_url TYPE string VALUE `https://software-heroes.com/api/v1/rest-test/partner`.

cl_http_client=>create_by_url( EXPORTING  url                = c_url
                               IMPORTING  client             = DATA(lo_client)
                               EXCEPTIONS argument_not_found = 1
                                          plugin_not_active  = 2
                                          internal_error     = 3
                                          OTHERS             = 4 ).

 

The next step is to supply the parameters of the URL. In the case of the preview API with the method to be executed and a data packet containing the test data. We also set the method to POST to generate a POST request for the API. Once all the data has been supplied, you can send the request via the client and the destination URL is called.

DATA(ld_data) = |\{| &
                |    "NAME": "SAP",| &
                |    "DESCRIPTION": "Software from Walldorf",| &
                |    "STATUS": "B",| &
                |    "PRICE": 1500.99,| &
                |    "CURRENCY": "EUR"| &
                |\}|.

lo_client->request->set_method( if_http_request=>co_request_method_post ).
lo_client->request->set_header_field( name  = 'Content-Type' value = 'application/json' ).
lo_client->request->set_header_field( name  = 'Swh-API-Key' value = c_key ).
lo_client->request->set_cdata( ld_data ).

lo_client->send( EXCEPTIONS http_communication_failure = 1
                            http_invalid_state         = 2
                            http_processing_failed     = 3
                            OTHERS                     = 4 ).

 

After the request, the result should be read. Through the response instance, the data can be read in character form, the data is readable and not in binary form.

lo_client->receive(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    OTHERS                     = 4 ).

DATA(ld_stream) = lo_client->response->get_cdata( ).

 

At the end of processing, the client's connection must be closed and completed. However, other requests can also be sent to the URL if further communication is necessary.

lo_client->close( ).

 

Full Example

Here is the full example from above. Maybe you will run in some trouble calling the REST endpoint, because of your systems setting or handshake problems. Here you can play around with other examples from the web.

CONSTANTS c_url TYPE string VALUE `https://software-heroes.com/api/v1/rest-test/partner`.
CONSTANTS c_key TYPE string VALUE `66703~97215F268193~65784`.

cl_http_client=>create_by_url( EXPORTING  url                = c_url
                               IMPORTING  client             = DATA(lo_client)
                               EXCEPTIONS argument_not_found = 1
                                          plugin_not_active  = 2
                                          internal_error     = 3
                                          OTHERS             = 4 ).

DATA(ld_data) = |\{| &
                |    "NAME": "SAP",| &
                |    "DESCRIPTION": "Software from Walldorf",| &
                |    "STATUS": "B",| &
                |    "PRICE": 1500.99,| &
                |    "CURRENCY": "EUR"| &
                |\}|.

lo_client->request->set_method( if_http_request=>co_request_method_post ).
lo_client->request->set_header_field( name  = 'Content-Type' value = 'application/json' ).
lo_client->request->set_header_field( name  = 'Swh-API-Key' value = c_key ).
lo_client->request->set_cdata( ld_data ).

lo_client->send( EXCEPTIONS http_communication_failure = 1
                            http_invalid_state         = 2
                            http_processing_failed     = 3
                            OTHERS                     = 4 ).

lo_client->receive( EXCEPTIONS http_communication_failure = 1
                               http_invalid_state         = 2
                               http_processing_failed     = 3
                               OTHERS                     = 4 ).

DATA(ld_stream) = lo_client->response->get_cdata( ).

lo_client->close( ).

 

Conclusion

After the request has been returned, the result must be processed as JSON or XML so that the report can process the data further. When calling our interface, the result is returned as JSON. How to convert a JSON string into a correct format in SAP, we will show you in another quick tip.


Included topics:
QuickPOST Request
Comments (4)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Tuesday and Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP in Practice - Creating a Setup App

Category - ABAP

No more SAP GUI? How do we then create a static setup app while still remaining flexible on the ABAP side for further implementation? Here's a practical example.

08/28/2026

DDIC of the Future (Comparison)

Category - ABAP

What does the future hold for the DDIC, and can we already use the new features today? In this article, we compare the world of today with that of tomorrow and show you where to find all the features.

07/03/2026

DDIC of the Future (Tomorrow)

Category - ABAP

What does the future hold for the DDIC, and can we already use its new features today? In this article, we'll take a look at tomorrow and how CDS artifacts can help us with modeling.

06/30/2026

DDIC of the Future (Today)

Category - ABAP

What does the future hold for the DDIC, and can we already use the new features today? In this article, we'll look at the current state of the dictionary in ABAP.

06/26/2026

ABAP Tools - VS Code (Quick Start)

Category - ABAP

The ABAP Development Tools for VS Code are now available, and in this short guide we'll look at everything you need to get started.

05/30/2026