HTTP Put – Put a file on a web server
This function use the PUT method of HTTP 1.0 protocol to send a file on a web server. It delete the file before the copy if already exists.
ABAP
FUNCTION z_http_put.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(URI) TYPE STRING
*" VALUE(FILE_CONTENT) TYPE XSTRING OPTIONAL
*" VALUE(CONTENT_TYPE) TYPE STRING OPTIONAL
*" EXPORTING
*" VALUE(CODE) TYPE I
*" VALUE(REASON) TYPE STRING
*" EXCEPTIONS
*" NO_LOGIN_ACCOUNT_AVAILABLE
*" HTTP_COMMUNICATION_FAILURE
*" HTTP_INVALID_STATE
*" HTTP_PROCESSING_FAILED
*" OTHERS
*"----------------------------------------------------------------------
* Core by MAX 2007 - Implementation by IperCube 2007
*"----------------------------------------------------------------------
DATA: user TYPE string,
pwd TYPE string,
x_bytes TYPE i.
* If no Content, exit
IF file_content IS INITIAL.
EXIT.
ENDIF.
* Set User/Password Logon to web server
user = 'my_user'.
pwd = 'my_password'.
* Translate URL
TRANSLATE uri USING ' *'.
REPLACE ALL OCCURRENCES OF '*' IN uri WITH '%20' IN CHARACTER MODE.
* Content type, default pdf (example)
IF content_type IS INITIAL.
content_type = 'application/pdf'.
ENDIF.
***************************************************
* Delete file (if exists)
***************************************************
DATA: client TYPE REF TO if_http_client,
timeout TYPE i VALUE 0.
* Create HTTP_CLIENT object
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = uri
IMPORTING
client = client.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Authentication
CALL METHOD client->authenticate
EXPORTING
* PROXY_AUTHENTICATION = ' '
* CLIENT = sy-mandt
username = user
password = pwd
* LANGUAGE = 'E'
.
* Set Header Fields - method DELETE
CALL METHOD client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'DELETE'.
CALL METHOD client->request->set_header_field
EXPORTING
name = '~server_protocol'
value = 'HTTP/1.0'.
CALL METHOD client->request->set_header_field
EXPORTING
name = 'content-type'
value = content_type.
* send/receive with HTTP server
CALL METHOD client->send
EXPORTING
timeout = timeout
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CASE sy-subrc.
WHEN 1. RAISE http_communication_failure.
WHEN 2. RAISE http_invalid_state.
WHEN 3. RAISE http_processing_failed.
WHEN 4. RAISE others.
ENDCASE.
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CASE sy-subrc.
WHEN 1. RAISE http_communication_failure.
WHEN 2. RAISE http_invalid_state.
WHEN 3. RAISE http_processing_failed.
WHEN 4. RAISE others.
ENDCASE.
* close connection
CALL METHOD client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
***************************************************
* PUT file to web server
***************************************************
CALL METHOD client->authenticate
EXPORTING
* PROXY_AUTHENTICATION = ' '
* CLIENT = sy-mandt
username = user
password = pwd
* LANGUAGE = 'E'
.
* Set Header Fields - method PUT
CALL METHOD client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'PUT'.
CALL METHOD client->request->set_header_field
EXPORTING
name = '~server_protocol'
value = 'HTTP/1.0'.
CALL METHOD client->request->set_header_field
EXPORTING
name = 'content-type'
value = content_type.
* Get stream lenght and set in request
x_bytes = XSTRLEN( file_content ).
CALL METHOD client->request->set_data
EXPORTING
data = file_content
length = x_bytes.
* send /receive with HTTP server
CALL METHOD client->send
EXPORTING
timeout = timeout
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CASE sy-subrc.
WHEN 1. RAISE http_communication_failure.
WHEN 2. RAISE http_invalid_state.
WHEN 3. RAISE http_processing_failed.
WHEN 4. RAISE others.
ENDCASE.
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CASE sy-subrc.
WHEN 1. RAISE http_communication_failure.
WHEN 2. RAISE http_invalid_state.
WHEN 3. RAISE http_processing_failed.
WHEN 4. RAISE others.
ENDCASE.
* Get Message
CALL METHOD client->response->get_status
IMPORTING
code = code
reason = reason.
* close connection
CALL METHOD client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
CASE sy-subrc.
WHEN 1. RAISE http_invalid_state.
WHEN 2. RAISE others.
ENDCASE.
ENDFUNCTION.