Skip to content

Instantly share code, notes, and snippets.

@crazko
Last active Aug 29, 2015
Embed
What would you like to do?
Starting code for handle_request method
CONSTANTS:
c_get TYPE string VALUE 'GET',
c_post TYPE string VALUE 'POST',
c_put TYPE string VALUE 'PUT',
c_delete TYPE string VALUE 'DELETE'.
TYPES:
BEGIN OF local_type_response,
success TYPE string,
msg TYPE string,
data TYPE " your table type,
END OF local_type_response.
DATA:
* lo_json_serializer TYPE REF TO cl_trex_json_serializer,
lo_json TYPE REF TO zcl_json_document,
lv_json TYPE string,
ls_response TYPE local_type_response,,
lv_verb TYPE string,
lv_path_info TYPE string,
lv_resource TYPE string,
lv_param1 TYPE string,
lv_param2 TYPE string.
lv_verb = server->request->get_header_field( name = '~request_method' ).
lv_path_info = server->request->get_header_field( name = '~path_info' ).
SHIFT lv_path_info LEFT BY 1 PLACES.
SPLIT lv_path_info AT '/' INTO lv_resource lv_param1 lv_param2.
CASE lv_verb.
WHEN c_get. " R - Read
WHEN c_post. " C - Create
WHEN c_put.
WHEN c_delete.
WHEN OTHERS.
server->response->set_status(
code = '405'
reason = 'Method not allowed'
).
server->response->set_header_field(
name = 'Allow'
value = 'GET, POST, PUT, DELETE'
).
EXIT.
ENDCASE.
* CREATE OBJECT lo_json_serializer
* EXPORTING
* data = ls_response.
*
* lo_json_serializer->serialize( ).
* lo_json_serializer->get_data( RECEIVING rval = lv_json ).
* lo_json = zcl_json_document=>create_with_data( ls_response ).
lo_json = zcl_json_document=>create_with_data( lv_verb ).
lv_json = lo_json->get_json( ).
server->response->set_header_field(
EXPORTING
name = 'Content-Type'
value = 'application/json; charset=iso-8859-1'
).
server->response->set_cdata( data = lv_json ).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment