*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IV_URL) TYPE STRING OPTIONAL "URI
*" VALUE(IV_CONTENT_TYPE) TYPE STRING OPTIONAL "*Content type, for example 'application/json'
*" VALUE(IV_SET_CDATA) TYPE STRING OPTIONAL "HTTP Request body
*" VALUE(AUTHORIZATION) TYPE STRING OPTIONAL "Authorization: Bearer <accessToken>
*" EXPORTING
*" VALUE(RESPONSE_BODY) TYPE STRING "Delivers the HTTP body of this entity as character data
*" VALUE(DATA_LENGTH) TYPE I "Supplies the length in bytes of the HTTP body
*" VALUE(HTTP_STATUS) TYPE STRING "HTTP Status Code&description
*"----------------------------------------------------------------------
"本函数通过if_http_client类实现HTTP POST,过程如下:
"1.创建连接
"2.发送请求
"3.接收返回
"4.关闭连接
data:lv_token type string.
DATA:
lo_http_client TYPE REF TO if_http_client,
lv_url TYPE string,
lv_response TYPE string,
lv_http_code TYPE i,
lv_http_reason TYPE string.
lv_url = iv_url.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
* proxy_host =
* proxy_service =
* ssl_id =
* sap_username =
* sap_client =
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CONCATENATE 'Bearer' AUTHORIZATION INTO lv_token SEPARATED BY space.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Authorization'
value = lv_token.
lo_http_client->request->set_method( 'POST' ).
lo_http_client->request->set_content_type( iv_content_type ).
lo_http_client->request->set_cdata(
EXPORTING
data = iv_set_cdata
* offset = 0
* length = -1
).
lo_http_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ).
lo_http_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
lv_response = lo_http_client->response->get_cdata( ).
"Supplies the length in bytes of the HTTP body
lo_http_client->response->if_http_entity~get_data_length(
IMPORTING
data_length = data_length ).
"Gets the current HTTP status code
lo_http_client->response->get_status(
IMPORTING
code = lv_http_code
reason = lv_http_reason ).
http_status = lv_http_code && lv_http_reason.
CONDENSE http_status NO-GAPS.
response_body = lv_response.
lo_http_client->close( ).