cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP Integration with FastAPI for File Uploads: Seeking Code Guidance

naveenkumarb142
Product and Topic Expert
Product and Topic Expert
0 Kudos
752

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:

  1. 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.

  2. 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:

  1. HTTP POST Request: The ABAP code should construct an HTTP POST request to the FastAPI endpoint.

  2. Multipart Form Data: The file should be sent as multipart form data in the body of the POST request.

    curl -X POST -F "file=@data.json" https://endpoint.cfapps.us10-001.hana.ondemand.com/upload/ This curl works on vscode or command prompt. I want to achieve same from ABAP. 

    Server accepts data file containing JSON, does processing and returns JSON. Sever side is python.
    #if_http_client #abap #btp #httprequests #fileuploads #Post @Tomas_Buryanek I saw your answer for similar query on image upload to servicenow. Kindly help with this, seeking your expertise.

    Regards,
    Naveen
View Entire Topic
naveenkumarb142
Product and Topic Expert
Product and Topic Expert
0 Kudos
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( ).