on 2024 Mar 02 1:11 PM
Problem Statement:
I am working on integrating an ABAP system with a FastAPI server to upload files via HTTP POST requests. I have a FastAPI endpoint set up to receive file uploads, and I need assistance with creating the ABAP code to send a file to this endpoint.
Description:
FastAPI Endpoint: I have a FastAPI endpoint (POST /upload/) set up to receive file uploads. The endpoint expects the file to be included in the body of the request.
ABAP System: On the ABAP side, I need to write code that reads a file from the filesystem and sends it to the FastAPI endpoint via an HTTP POST request.
Requirements:
HTTP POST Request: The ABAP code should construct an HTTP POST request to the FastAPI endpoint.
Multipart Form Data: The file should be sent as multipart form data in the body of the POST request.
DATA: lv_url TYPE string VALUE " ",
lo_http_client TYPE REF TO if_http_client,
lv_text TYPE string,
lv_json_text TYPE string,
lv_jsonx_text TYPE xstring,
lv_jsonx TYPE xstring,
lo_part TYPE REF TO if_http_entity.
* Create XML reader
DATA(lo_reader) = cl_sxml_string_reader=>create( cl_abap_codepage=>convert_to( iv_json ) ).
TRY.
lo_reader->next_node( ).
lo_reader->skip_node( ).
CATCH cx_sxml_parse_error INTO DATA(lx_parse_error). cl_demo_output=>display( lx_parse_error->get_text( ) ).
ENDTRY.
* Create HTTP client
cl_http_client=>create_by_url( EXPORTING url = lv_url IMPORTING client = lo_http_client ).
* Prepare request
lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
lo_http_client->request->set_content_type( 'multipart/form-data' ). lo_http_client->request->set_form_field( name = 'File' value = 'data.json' ). lo_http_client->request->set_form_field( name = 'Text' value = 'Cluster based on Customer' ).
lv_text = to_lower( iv_bundle_criteria ).
lv_json_text = '{"placeholder"}'.
REPLACE 'placeholder' IN CHARACTER MODE WITH lv_text INTO lv_json_text. lv_jsonx_text = cl_abap_codepage=>convert_to( source = lv_json_text codepage = 'UTF-8' ).
DATA(json_lenx_text) = xstrlen( lv_jsonx_text ).
lv_jsonx = cl_abap_codepage=>convert_to( source = iv_json codepage = 'UTF-8' ).
DATA(json_lenx) = xstrlen( lv_jsonx ).
* Add multipart form data - File Data
lo_part = lo_http_client->request->if_http_entity~add_multipart( ). lo_part->set_header_field( name = 'content-disposition' value = 'form-data; name="file"; filename="data.json"' ).
lo_part->set_data( data = lv_jsonx offset = 0 length = json_lenx ).
* Add multipart form data - Text Data
lo_part = lo_http_client->request->if_http_entity~add_multipart( ). lo_part->set_header_field( name = 'content-disposition' value = 'form-data; name="text"; value="Cluster based on Customer"' ).
lo_part->set_data( data = lv_jsonx_text offset = 0 length = json_lenx_text ).
* Send and receive data
lo_http_client->send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 ).
CHECK sy-subrc = 0.
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
* Response Data.
rv_response = lo_http_client->response->get_cdata( ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
63 | |
10 | |
8 | |
7 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.