Skip to Content
Technical Articles
Author's profile photo Johan Wigert

HTTP status codes in ABAP

I’ve been working a lot with OData and REST lately, outbound as well as inbound. Some of my code needs to handle HTTP status codes in different ways. Since I didn’t want to hard-code the status codes in my code, I started looking around for standard classes and interfaces which could be of use to me. This blog post is a brief summary of what I found.

Interface IF_HTTP_STATUS

The interface IF_HTTP_STATUS contains a list of status texts in the form of constants of data type STRING. Some examples are:

CONSTANTS reason_200 TYPE string VALUE 'OK' . "#EC NOTEXT
CONSTANTS reason_201 TYPE string VALUE 'Created' . "#EC NOTEXT

 

This interface is quite handy if you want to present the user with not only a status code but also the corresponding text.

Class CL_REST_STATUS_CODE

The classes CL_REST_STATUS_CODE and /IWCOR/CL_REST_STATUS_CODE also contain constants. The two classes are identical in regard to the constants but have some minor differences in one of the methods.

The constants in these two classes contain the numerical HTTP status codes of data type I. Some examples are:

constants GC_SUCCESS_OK type I value 200 . "#EC NOTEXT
constants GC_SUCCESS_CREATED type I value 201 . "#EC NOTEXT

 

The classes have some useful static methods as well:

  • Method GET_REASON_PHRASE: Takes in a status code and returns the corresponding status text. The method actually uses interface IF_HTTP_STATUS to accomplish this.
  • Method IS_CLIENT_ERROR: Takes in a status code and returns abap_true if the status code is in the range 400 – 499. Otherwise, the method returns abap_false.
  • Method IS_REDIRECTION: Takes in a status code and returns abap_true if the status code is in the range 300 – 399. Otherwise, the method returns abap_false.
  • Method IS_SUCCESS: Takes in a status code and returns abap_true if the status code is in the range 200 – 299. Otherwise, the method returns abap_false.
  • Method IS_SERVER_ERROR: Takes in a status code and returns abap_true if the status code is in the range 500 – 599. Otherwise, the method returns abap_false.
  • Method IS_ERROR: If one of the methods IS_CLIENT_ERROR and IS_SERVER_ERROR return abap_true, this method also returns abap_true. Otherwise, the method returns abap_false.

Limitations

The interface and the two classes mentioned in this blog post don’t contain constants for every HTTP status code mentioned in the Wikipedia article. However the most common ones are available, and for the use cases I’ve experienced so far the interface and the classes have been sufficient.

Happy coding!

This blog post first appeared on the Developer Voyage blog at https://www.developervoyage.com/2019/11/16/http-status-codes-in-abap.html

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Valdir Mendes
      Valdir Mendes

      Thanks a lot!

      Author's profile photo Sérgio Fraga
      Sérgio Fraga

      Hi Johan Wigert ,

      Thank you for sharing these little ABAP secrets.

      Just discovered a nice interface with all HTTP methods: IF_REST_MESSAGE

       

      Author's profile photo Johan Wigert
      Johan Wigert
      Blog Post Author

      Thanks for sharing! That is definitely also a useful interface!