05-11-2018 8:05 PM - edited 02-04-2024 3:11 AM
Hi Experts,
I am trying to use Leonardo API of image classification in ABAP, but on the receive method of lo_http_client, I am facing http_communication_failure. Do I need to expose/open any port in the system to access that URL of API?
URL: https://sandbox.api.sap.com/ml/imageclassifier/inference_sync
Code:
DATA: lo_http_client TYPE REF TO if_http_client.
DATA: response TYPE string.
CALL METHOD cl_http_client=>create_by_url EXPORTING url = 'https://sandbox.api.sap.com/ml/imageclassifier/inference_sync' IMPORTING client = lo_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. ENDIF. lo_http_client->request->set_method('POST'). lo_http_client->request->set_header_field( name = 'content-type' value = 'multipart/form-data; boundary=---011000010111000001101001' ). lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ). lo_http_client->request->set_header_field( name = 'APIKey' value = '<API_KEY>' ). DATA: l_xstring TYPE xstring. DATA: binary_tab TYPE STANDARD TABLE OF x255. DATA: file_length type i. CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = '<file_path>' filetype = 'BIN' IMPORTING filelength = file_length TABLES data_tab = binary_tab. CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = file_length IMPORTING buffer = l_xstring TABLES binary_tab = binary_tab EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. ENDIF. lo_http_client->request->append_cdata( `-----011000010111000001101001` && cl_abap_char_utilities=>cr_lf && `Content-Disposition: form-data; name="files"; filename="<file_name>";` && cl_abap_char_utilities=>cr_lf && cl_abap_char_utilities=>cr_lf ). lo_http_client->request->append_data( l_xstring ). lo_http_client->request->append_cdata( cl_abap_char_utilities=>cr_lf && `-----011000010111000001101001--` ).
CALL METHOD lo_http_client->send
EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc = 0.
CALL METHOD lo_http_client->receive
EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 5.
ENDIF.
IF sy-subrc <> 0.
ENDIF.
response = lo_http_client->response->get_cdata( ).
WRITE: 'response: ', response.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.