SAP ABAP HTTP
From SapWiki
Contents
Ejemplo HTTP_CLIENT
Ejemplo con SOAP
*URL https://qa.sapwiki.cl:443/polosur/DLEApi.php *NAMESPACE https://qa.sapwiki.cl/polosur/ *HOST qa.sapwiki.cl *SOAP ACTION https://qa.sapwiki.cl/polosur/CargarDocumento *----------------------------------------------------------------------* * FORM http_client *----------------------------------------------------------------------* * basado en programa RSWF_TEST_HTTP *----------------------------------------------------------------------* * --> p_url Ej.: https://qa.sapwiki.cl:443/polosur/DLEApi.php * --> p_soap String <soapenv:Envelope...</soapenv:Envelope> * --> p_soap_action Parámetro SOAPAction: * --> p_host Parámetro Host: * <-- p2 text *----------------------------------------------------------------------* FORM http_client USING p_url p_soap p_soap_action p_host CHANGING p_subrc p_msg p_response. TYPE-POOLS: swfxc, icon. DATA: l_http_client TYPE REF TO if_http_client. DATA: l_url TYPE string. DATA: l_code TYPE sy-subrc. DATA: l_code_string TYPE string. DATA: l_message_string TYPE string. DATA: lt_http_fields TYPE tihttpnvp. DATA: l_http_field_wa TYPE ihttpnvp. DATA: l_char_header(40) TYPE c. DATA: l_body_string TYPE string. *----------------------------------------------------------------------- *- Create the HTTP-Client *----------------------------------------------------------------------- CALL METHOD cl_http_client=>create_by_url EXPORTING url = p_url * PROXY_HOST = l_proxy_host * PROXY_SERVICE = l_proxy_service ssl_id = 'ANONYM' IMPORTING client = l_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 * into l_message_string. IF l_message_string IS INITIAL. l_message_string = 'Error creating HTTP-Client'. "#EC NOTEXT ENDIF. * WRITE: / icon_red_light AS ICON. * PERFORM print_string USING l_message_string. p_msg = l_message_string. p_subrc = 9. EXIT. ENDIF. *--------------------------------------------------------------------* * HEADER *--------------------------------------------------------------------* * CALL METHOD l_http_client->request->set_header_field * EXPORTING * name = '~request_method' * value = 'POST'. DATA(l_bytes) = strlen( p_soap ). DATA l_content_length TYPE string. MOVE l_bytes TO l_content_length. CONDENSE l_content_length. CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'Accept-Encoding' value = 'gzip,deflate'. "#EC NOTEXT CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'Content-Type' value = 'text/xml;charset=UTF-8'. "#EC NOTEXT CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'SOAPAction' value = p_soap_action. "#EC NOTEXT "'"https://qa.sapwiki.cl/polosur/CargarDocumento"'. CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'Host' value = p_host. "#EC NOTEXT "'qa.sapwiki.cl'. CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'Connection' value = 'Keep-Alive'. "#EC NOTEXT CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'User-Agent' value = 'SAP NetWeaver Application Server (1.0;740)'. "#EC NOTEXT CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'Content-Length' value = l_content_length. "#EC NOTEXT "'30000'. *--------------------------------------------------------------------* * soapenvelope * string <soapenv:Envelope...</soapenv:Envelope> *--------------------------------------------------------------------* CALL METHOD l_http_client->request->set_cdata EXPORTING data = p_soap. * offset = 0 * length = '19000'. ** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V ** provide the compression settings to the HTTP client object ** a) compress request * IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true. * CALL METHOD l_http_client->set_compression * EXPORTING * options = if_http_client=>co_compress_in_all_cases * EXCEPTIONS * compression_not_possible = 1. "#EC * ** ignore this exception to have the same behaviour as for clients created ** via sm59-destination: for this clients the method set_compression is ** called within the method SEND without exception handling! * ENDIF. * * ** b) compress response * IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true. * l_http_client->propertytype_accept_compress = if_http_client=>co_enabled. * ELSE. ** m_client->propertytype_accept_compress = if_http_client=>co_disabled. * ENDIF. * ** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8) ** the HTTP client is not allowed to follow an redirect automatically ** for methods other than HEAD and GET => prevent this for WS calls ** NOTE: prevent the redirect also for PING functionalty although HEAD is ** used in this case in order to get an reliable PING-result * l_http_client->propertytype_redirect = if_http_client=>co_disabled. *----------------------------------------------------------------------- *- send the http post *----------------------------------------------------------------------- CALL METHOD l_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2. IF sy-subrc <> 0. * WRITE: / icon_red_light AS ICON. CALL METHOD l_http_client->get_last_error IMPORTING code = l_code message = l_message_string. CALL METHOD l_http_client->close. l_code_string = l_code. CONCATENATE 'HTTP-Send: RC=' l_code_string "#EC NOTEXT INTO l_code_string . CONCATENATE l_code_string l_message_string INTO l_message_string SEPARATED BY space. * PERFORM print_string USING l_message_string. p_msg = l_message_string. p_subrc = 8. EXIT. ENDIF. *----------------------------------------------------------------------- *- receive the result of http post *----------------------------------------------------------------------- CALL METHOD l_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2. IF sy-subrc <> 0. * WRITE: / icon_red_light AS ICON. CALL METHOD l_http_client->get_last_error IMPORTING code = l_code message = l_message_string. CALL METHOD l_http_client->close. l_code_string = l_code. CONCATENATE 'HTTP-Receive: RC=' l_code_string "#EC NOTEXT INTO l_code_string . CONCATENATE l_code_string l_message_string INTO l_message_string SEPARATED BY space. * PERFORM print_string USING l_message_string. p_msg = l_message_string. p_subrc = 7. EXIT. ENDIF. *----------------------------------------------------------------------- *- print the results *----------------------------------------------------------------------- CALL METHOD l_http_client->response->get_status IMPORTING code = l_code. IF l_code < 300. * HTTP-codes: 100 - 199 = informations * HTTP-codes: 200 - 299 = client-request successful (200 = OK) * WRITE: / icon_green_light AS ICON. p_subrc = 0. ELSE. * HTTP-codes: 300 - 399 = redirected; further actions required * HTTP-codes: 400 - 499 = client-request incomplete * HTTP-codes: 500 - 599 = server errors * WRITE: / icon_red_light AS ICON. * IF l_code BETWEEN 300 AND 399. * p_msg = 'Redirected; further actions required'. "#EC NOTEXT * ELSEIF l_code BETWEEN 400 AND 499. * p_msg = 'Client-request incomplete'. "#EC NOTEXT * ELSE. * p_msg = 'Server errors'. "#EC NOTEXT * ENDIF. * * p_msg = |Web Service: Error { l_code } { p_msg }|. "#EC NOTEXT p_subrc = 6. ENDIF. *-- get the http header fields * CALL METHOD l_http_client->response->get_header_fields * CHANGING * fields = lt_http_fields. * LOOP AT lt_http_fields INTO l_http_field_wa. * l_char_header = l_http_field_wa-name. * WRITE: / l_char_header. * l_char_header = l_http_field_wa-value. * WRITE: l_char_header. * ENDLOOP. * WRITE: / sy-uline. *- get the body IF p_subrc = 0. l_body_string = l_http_client->response->get_cdata( ). * PERFORM print_string USING l_body_string. p_response = l_body_string. ELSE. *-- get the http header fields CALL METHOD l_http_client->response->get_header_fields CHANGING fields = lt_http_fields. LOOP AT lt_http_fields INTO l_http_field_wa. p_msg = |Webservice: Error { l_http_field_wa-value }|. EXIT. ENDLOOP. ENDIF. ENDFORM.
Ejemplo con POST
METHOD http_client. TYPE-POOLS: swfxc, icon. DATA: l_http_client TYPE REF TO if_http_client. DATA: l_url TYPE string. DATA: l_code TYPE sy-subrc. DATA: l_code_string TYPE string. DATA: l_message_string TYPE string. DATA: lt_http_fields TYPE tihttpnvp. DATA: l_http_field_wa TYPE ihttpnvp. DATA: l_char_header(40) TYPE c. DATA: l_body_string TYPE string. *----------------------------------------------------------------------- *- Create the HTTP-Client *----------------------------------------------------------------------- CALL METHOD cl_http_client=>create_by_url EXPORTING url = i_url * PROXY_HOST = l_proxy_host * PROXY_SERVICE = l_proxy_service ssl_id = 'ANONYM' IMPORTING client = l_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 * into l_message_string. IF l_message_string IS INITIAL. l_message_string = 'Error creating HTTP-Client'. "#EC NOTEXT ENDIF. * WRITE: / icon_red_light AS ICON. * PERFORM print_string USING l_message_string. e_msg = l_message_string. e_subrc = 9. EXIT. ENDIF. *--------------------------------------------------------------------* * HEADER *--------------------------------------------------------------------* * l_http_client->request->set_method( 'POST' ). l_http_client->request->set_method( i_method ). * l_http_client->request->set_content_type( 'application/json' ). IF i_content_type IS NOT INITIAL. l_http_client->request->set_content_type( i_content_type ). ENDIF. IF i_x_api_key IS SUPPLIED. CALL METHOD l_http_client->request->set_header_field EXPORTING name = i_x_api_key value = i_api_key. "#EC NOTEXT ELSE. CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'X-API-KEY' value = i_api_key. "#EC NOTEXT ENDIF. IF i_content_type IS NOT INITIAL. CALL METHOD l_http_client->request->set_header_field EXPORTING name = 'Content-Type' value = 'application/json'. "#EC NOTEXT ENDIF. *--------------------------------------------------------------------* * soapenvelope * string <soapenv:Envelope...</soapenv:Envelope> *--------------------------------------------------------------------* IF i_body IS SUPPLIED. CALL METHOD l_http_client->request->set_cdata EXPORTING data = i_body. ENDIF. * offset = 0 * length = '19000'. ** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V ** provide the compression settings to the HTTP client object ** a) compress request * IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true. * CALL METHOD l_http_client->set_compression * EXPORTING * options = if_http_client=>co_compress_in_all_cases * EXCEPTIONS * compression_not_possible = 1. "#EC * ** ignore this exception to have the same behaviour as for clients created ** via sm59-destination: for this clients the method set_compression is ** called within the method SEND without exception handling! * ENDIF. * * ** b) compress response * IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true. * l_http_client->propertytype_accept_compress = if_http_client=>co_enabled. * ELSE. ** m_client->propertytype_accept_compress = if_http_client=>co_disabled. * ENDIF. * ** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8) ** the HTTP client is not allowed to follow an redirect automatically ** for methods other than HEAD and GET => prevent this for WS calls ** NOTE: prevent the redirect also for PING functionalty although HEAD is ** used in this case in order to get an reliable PING-result * l_http_client->propertytype_redirect = if_http_client=>co_disabled. *----------------------------------------------------------------------- *- send the http post *----------------------------------------------------------------------- CALL METHOD l_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2. IF sy-subrc <> 0. * WRITE: / icon_red_light AS ICON. CALL METHOD l_http_client->get_last_error IMPORTING code = l_code message = l_message_string. CALL METHOD l_http_client->close. l_code_string = l_code. CONCATENATE 'HTTP-Send: RC=' l_code_string "#EC NOTEXT INTO l_code_string . CONCATENATE l_code_string l_message_string INTO l_message_string SEPARATED BY space. * PERFORM print_string USING l_message_string. e_msg = l_message_string. e_subrc = 8. EXIT. ENDIF. *----------------------------------------------------------------------- *- receive the result of http post *----------------------------------------------------------------------- CALL METHOD l_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2. IF sy-subrc <> 0. * WRITE: / icon_red_light AS ICON. CALL METHOD l_http_client->get_last_error IMPORTING code = l_code message = l_message_string. CALL METHOD l_http_client->close. l_code_string = l_code. CONCATENATE 'HTTP-Receive: RC=' l_code_string "#EC NOTEXT INTO l_code_string . CONCATENATE l_code_string l_message_string INTO l_message_string SEPARATED BY space. * PERFORM print_string USING l_message_string. e_msg = l_message_string. e_subrc = 7. EXIT. ENDIF. *----------------------------------------------------------------------- *- print the results *----------------------------------------------------------------------- CALL METHOD l_http_client->response->get_status IMPORTING code = l_code. IF l_code < 300 OR l_code = 400. "bad request * HTTP-codes: 100 - 199 = informations * HTTP-codes: 200 - 299 = client-request successful (200 = OK) * WRITE: / icon_green_light AS ICON. e_subrc = 0. ELSE. * HTTP-codes: 300 - 399 = redirected; further actions required * HTTP-codes: 400 - 499 = client-request incomplete * HTTP-codes: 500 - 599 = server errors * WRITE: / icon_red_light AS ICON. * IF l_code BETWEEN 300 AND 399. * p_msg = 'Redirected; further actions required'. "#EC NOTEXT * ELSEIF l_code BETWEEN 400 AND 499. * p_msg = 'Client-request incomplete'. "#EC NOTEXT * ELSE. * p_msg = 'Server errors'. "#EC NOTEXT * ENDIF. * * p_msg = |Cloudsigner: Error { l_code } { p_msg }|. "#EC NOTEXT e_subrc = 6. ENDIF. *-- get the http header fields * CALL METHOD l_http_client->response->get_header_fields * CHANGING * fields = lt_http_fields. * LOOP AT lt_http_fields INTO l_http_field_wa. * l_char_header = l_http_field_wa-name. * WRITE: / l_char_header. * l_char_header = l_http_field_wa-value. * WRITE: l_char_header. * ENDLOOP. * WRITE: / sy-uline. *- get the body IF e_subrc = 0. l_body_string = l_http_client->response->get_cdata( ). * PERFORM print_string USING l_body_string. e_response = l_body_string. ELSE. *-- get the http header fields CALL METHOD l_http_client->response->get_header_fields CHANGING fields = lt_http_fields. LOOP AT lt_http_fields INTO l_http_field_wa. e_msg = |Acepta: Error { l_http_field_wa-value }|. EXIT. ENDLOOP. ENDIF. ENDMETHOD.
Llamada a proxy client=
* proxy client DATA: lo_proxy TYPE REF TO zhcm_co_si_os_carga_documento, ls_output TYPE zhcm_mt_carga_documento, ls_input TYPE zhcm_mt_respuesta_documento, lo_system_ex TYPE REF TO cx_ai_system_fault, lo_app_ex TYPE REF TO cx_ai_application_fault. *do something *..... * TRY. * create proxy CREATE OBJECT lo_proxy. CALL METHOD lo_proxy->si_os_carga_documento EXPORTING output = ls_output IMPORTING input = ls_input. CATCH cx_ai_system_fault INTO lo_system_ex. * WRITE lo_system_ex->errortext TO msg. * msg = lo_system_ex->if_message~get_text( ). CATCH cx_ai_application_fault INTO lo_app_ex. * WRITE lo_app_ex->textid TO msg. * msg = lo_app_ex->if_message~get_text( ). ENDTRY. IF lo_system_ex IS NOT INITIAL. e_message = lo_system_ex->if_message~get_text( ). e_subrc = 1. RETURN. ENDIF. IF lo_app_ex IS NOT INITIAL. e_message = lo_app_ex->if_message~get_text( ). e_subrc = 2. RETURN. ENDIF.
Notas PROXY
- SRT_UTIL tran. log de llamada proxy
- Tabla SPROXXSL : relación entre proxy & XSLT hay que concatenar /1SAI/TXS & NAME e ir a la XSLT_TOOL
- Si en llamada a proxy da error de deserialization, poner break en CL_SXMLP_DATA_ST==============CM004 línea 26
- RSSIDL_DESERIALIZE_DEMO verificar WSDL, ver nota Ver nota 1801866 - Proxy generation terminated: Incorrect value: Unknown Type Reference
- Test conexión HTML, reporte RSWF_TEST_HTTP
- LOG ICM : transacción SMICM, en menú: Pasar a -->Fichero trace-->Visualizar todo
- Configuración Proxy : transacción SICF
- Nota 2469949 - "ICF Error when creating HTTP client object by Config for URL" in Web Services ABAP
- Nota 2334940 - B2A: Troubleshooting HTTPS for communication with health insurance funds
- Nota 1714792 - Proxy generation terminated: Message must have exactly one part
- Nota 1817447 - Error "GET_BUSINESS_SYSTEM_ERROR" for Consumer Proxy calls
- Nota 2033184 - How to create Logical Port manually for a Service Consumer in SOAMANAGER [Video]
- Nota 1791729 - WDA FileUpload error: "Error when uploading; the file name is invalid or the file is too large"
- Nota 2322379 - How to create payload trace using SRT_UTIL in AS ABAP
- Nota 2368112 - Outgoing HTTPS connection does not work in AS ABAP
- Nota 2388992 - WSDL parsing error: Exception in library handler
- Nota 2508091 - Web Services ABAP: Error "Element is missing" CX_ST_GROUP_MISSING_CASE
- Nota 2553979 - SOAP Web Services ABAP - Guided Answers