<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: ABAP Integration with FastAPI for File Uploads: Seeki... in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626142#M4820511</link>
    <description>if this is the solution, why don't you mark it as solved?</description>
    <pubDate>Sun, 03 Mar 2024 08:27:13 GMT</pubDate>
    <dc:creator>Andre_Fischer</dc:creator>
    <dc:date>2024-03-03T08:27:13Z</dc:date>
    <item>
      <title>ABAP Integration with FastAPI for File Uploads: Seeking Code Guidance</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaq-p/13625962</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Problem Statement:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Description:&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;FastAPI Endpoint:&lt;/STRONG&gt; 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.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;ABAP System:&lt;/STRONG&gt; 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.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;Requirements:&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;HTTP POST Request:&lt;/STRONG&gt; The ABAP code should construct an HTTP POST request to the FastAPI endpoint.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Multipart Form Data:&lt;/STRONG&gt; The file should be sent as multipart form data in the body of the POST request.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;curl -X POST -F "file=@data.json" &lt;A href="https://endpoint.cfapps.us10-001.hana.ondemand.com/upload/" target="_blank" rel="noopener"&gt;https://endpoint.cfapps.us10-001.hana.ondemand.com/upload/&lt;/A&gt;&amp;nbsp;This curl works on vscode or command prompt. I want to achieve same from ABAP.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Server accepts data file containing JSON, does processing and returns JSON. Sever side is python.&lt;BR /&gt;#if_http_client #abap #btp #httprequests #fileuploads #Post&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/6487"&gt;@Tomas_Buryanek&lt;/a&gt;&amp;nbsp;I saw your answer for similar query on image upload to servicenow. Kindly help with this, seeking your expertise.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Naveen&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Sat, 02 Mar 2024 13:11:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaq-p/13625962</guid>
      <dc:creator>naveenkumarb142</dc:creator>
      <dc:date>2024-03-02T13:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP Integration with FastAPI for File Uploads: Seeki...</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626006#M4820492</link>
      <description>&lt;P&gt;REPORT ztest_030324.&lt;/P&gt;&lt;P&gt;CONSTANTS:&lt;BR /&gt;lc_content_type TYPE string VALUE 'Content-Type'.&lt;/P&gt;&lt;P&gt;DATA:&lt;BR /&gt;ls_desc TYPE soli,&lt;BR /&gt;lv_response TYPE string,&lt;BR /&gt;lt_data TYPE TABLE OF string,&lt;BR /&gt;ls_data TYPE string,&lt;BR /&gt;lv_data TYPE string,&lt;BR /&gt;lv_filename TYPE string VALUE 'C:\DUMMY\FILE.JSON',&lt;BR /&gt;lv_url TYPE string VALUE '&lt;A href="https://dummy-endpoint.com/upload/" target="_blank"&gt;https://dummy-endpoint.com/upload/&lt;/A&gt;'.&lt;/P&gt;&lt;P&gt;* Data Declarations for http client.&lt;BR /&gt;DATA:&lt;BR /&gt;lo_http_client TYPE REF TO if_http_client,&lt;BR /&gt;lo_rest_client TYPE REF TO cl_rest_http_client.&lt;/P&gt;&lt;P&gt;* Data Declarations for http client header.&lt;BR /&gt;DATA:&lt;BR /&gt;lt_headers TYPE tihttpnvp,&lt;BR /&gt;ls_headers LIKE LINE OF lt_headers,&lt;BR /&gt;lo_request_part TYPE REF TO if_http_entity,&lt;BR /&gt;lv_content_disposition TYPE string.&lt;/P&gt;&lt;P&gt;* Get the current active screen as a screenshot.&lt;BR /&gt;CALL METHOD cl_gui_frontend_services=&amp;gt;gui_upload&lt;BR /&gt;EXPORTING&lt;BR /&gt;filename = lv_filename&lt;BR /&gt;CHANGING&lt;BR /&gt;data_tab = lt_data&lt;BR /&gt;EXCEPTIONS&lt;BR /&gt;access_denied = 1&lt;BR /&gt;error_no_gui = 3&lt;BR /&gt;not_supported_by_gui = 4&lt;BR /&gt;OTHERS = 5.&lt;/P&gt;&lt;P&gt;* Convert the table of strings to a single string.&lt;BR /&gt;LOOP AT lt_data INTO ls_data.&lt;BR /&gt;CONCATENATE lv_data ls_data INTO lv_data.&lt;BR /&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;DATA: lv_datax TYPE xstring.&lt;/P&gt;&lt;P&gt;CALL FUNCTION 'SCMS_STRING_TO_XSTRING'&lt;BR /&gt;EXPORTING&lt;BR /&gt;text = lv_data&lt;BR /&gt;IMPORTING&lt;BR /&gt;buffer = lv_datax&lt;BR /&gt;EXCEPTIONS&lt;BR /&gt;failed = 1&lt;BR /&gt;OTHERS = 2.&lt;BR /&gt;IF sy-subrc &amp;lt;&amp;gt; 0.&lt;BR /&gt;* Implement suitable error handling here&lt;BR /&gt;ENDIF.&lt;/P&gt;&lt;P&gt;cl_http_client=&amp;gt;create_by_url( EXPORTING url = lv_url IMPORTING client = lo_http_client ).&lt;BR /&gt;lo_http_client-&amp;gt;request-&amp;gt;set_header_field( name = 'Content-Type' value = 'multipart/form-data' ). "#EC NOTEXT&lt;BR /&gt;lo_request_part = lo_http_client-&amp;gt;request-&amp;gt;add_multipart( ).&lt;BR /&gt;lo_request_part-&amp;gt;set_content_type( 'application/xml' ).&lt;BR /&gt;lv_content_disposition = |form-data; name="file"; filename="data.json" |.&lt;BR /&gt;lo_request_part-&amp;gt;set_header_field( name = `Content-Disposition` value = lv_content_disposition ).&lt;BR /&gt;lo_request_part-&amp;gt;set_data( data = lv_datax ).&lt;/P&gt;&lt;P&gt;* Set method as post method.&lt;BR /&gt;lo_http_client-&amp;gt;request-&amp;gt;set_method(&lt;BR /&gt;EXPORTING&lt;BR /&gt;method = if_http_entity=&amp;gt;co_request_method_post ).&lt;/P&gt;&lt;P&gt;lo_http_client-&amp;gt;request-&amp;gt;set_content_type(&lt;BR /&gt;EXPORTING&lt;BR /&gt;content_type = 'multipart/form-data'&lt;BR /&gt;).&lt;/P&gt;&lt;P&gt;* Prepare header of the API call.&lt;BR /&gt;CREATE OBJECT lo_rest_client&lt;BR /&gt;EXPORTING&lt;BR /&gt;io_http_client = lo_http_client.&lt;/P&gt;&lt;P&gt;ls_headers-name = lc_content_type.&lt;BR /&gt;ls_headers-value = 'application/json'.&lt;BR /&gt;APPEND ls_headers TO lt_headers.&lt;/P&gt;&lt;P&gt;CALL METHOD lo_rest_client-&amp;gt;if_rest_client~set_request_headers&lt;BR /&gt;EXPORTING&lt;BR /&gt;it_header_fields = lt_headers.&lt;/P&gt;&lt;P&gt;* Send and receive the data to and from the dummy API endpoint.&lt;BR /&gt;lo_http_client-&amp;gt;send(&lt;BR /&gt;EXCEPTIONS&lt;BR /&gt;http_communication_failure = 1&lt;BR /&gt;http_invalid_state = 2 ).&lt;/P&gt;&lt;P&gt;CHECK sy-subrc = 0.&lt;BR /&gt;lo_http_client-&amp;gt;receive(&lt;BR /&gt;EXCEPTIONS&lt;BR /&gt;http_communication_failure = 1&lt;BR /&gt;http_invalid_state = 2&lt;BR /&gt;http_processing_failed = 3 ).&lt;/P&gt;&lt;P&gt;* Response manipulation to get the incident number.&lt;BR /&gt;lv_response = lo_http_client-&amp;gt;response-&amp;gt;get_cdata( ).&lt;/P&gt;&lt;P&gt;WRITE: / lv_response.&lt;/P&gt;&lt;P&gt;This is the ABAP code&lt;BR /&gt;Response -&amp;gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Response.png" style="width: 999px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/74557iE0114C968BF31DA9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Response.png" alt="Response.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2024 17:30:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626006#M4820492</guid>
      <dc:creator>naveenkumarb142</dc:creator>
      <dc:date>2024-03-02T17:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP Integration with FastAPI for File Uploads: Seeki...</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626142#M4820511</link>
      <description>if this is the solution, why don't you mark it as solved?</description>
      <pubDate>Sun, 03 Mar 2024 08:27:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626142#M4820511</guid>
      <dc:creator>Andre_Fischer</dc:creator>
      <dc:date>2024-03-03T08:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP Integration with FastAPI for File Uploads: Seeki...</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626146#M4820513</link>
      <description>Hi Andre,</description>
      <pubDate>Sun, 03 Mar 2024 08:29:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626146#M4820513</guid>
      <dc:creator>naveenkumarb142</dc:creator>
      <dc:date>2024-03-03T08:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP Integration with FastAPI for File Uploads: Seeki...</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626147#M4820514</link>
      <description>Hi Andre, This is not the solution, Its the code I am using and error am facing.</description>
      <pubDate>Sun, 03 Mar 2024 08:29:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13626147#M4820514</guid>
      <dc:creator>naveenkumarb142</dc:creator>
      <dc:date>2024-03-03T08:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP Integration with FastAPI for File Uploads: Seeki...</title>
      <link>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13782535#M4839820</link>
      <description>&lt;LI-CODE lang="abap"&gt;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=&amp;gt;create( cl_abap_codepage=&amp;gt;convert_to( iv_json ) ).     

TRY.         
lo_reader-&amp;gt;next_node( ).         
lo_reader-&amp;gt;skip_node( ).       
CATCH cx_sxml_parse_error INTO DATA(lx_parse_error).         cl_demo_output=&amp;gt;display( lx_parse_error-&amp;gt;get_text( ) ).     
ENDTRY.  

*   Create HTTP client     
cl_http_client=&amp;gt;create_by_url( EXPORTING url = lv_url IMPORTING client = lo_http_client ).  

*   Prepare request    
lo_http_client-&amp;gt;request-&amp;gt;set_method( if_http_request=&amp;gt;co_request_method_post ).     
lo_http_client-&amp;gt;request-&amp;gt;set_content_type( 'multipart/form-data' ).     lo_http_client-&amp;gt;request-&amp;gt;set_form_field( name = 'File' value = 'data.json' ).     lo_http_client-&amp;gt;request-&amp;gt;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=&amp;gt;convert_to( source = lv_json_text codepage = 'UTF-8' ).     
DATA(json_lenx_text) = xstrlen( lv_jsonx_text ).      
lv_jsonx  = cl_abap_codepage=&amp;gt;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-&amp;gt;request-&amp;gt;if_http_entity~add_multipart( ).     lo_part-&amp;gt;set_header_field( name = 'content-disposition' value = 'form-data; name="file"; filename="data.json"' ).     
lo_part-&amp;gt;set_data( data = lv_jsonx offset = 0  length = json_lenx ). 

*   Add multipart form data - Text Data     
lo_part = lo_http_client-&amp;gt;request-&amp;gt;if_http_entity~add_multipart( ).     lo_part-&amp;gt;set_header_field( name = 'content-disposition' value = 'form-data; name="text"; value="Cluster based on Customer"' ).     
lo_part-&amp;gt;set_data( data = lv_jsonx_text offset = 0  length = json_lenx_text ).  

*   Send and receive data     
lo_http_client-&amp;gt;send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 ).     
CHECK sy-subrc = 0.     
lo_http_client-&amp;gt;receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).  

*   Response Data.     
rv_response = lo_http_client-&amp;gt;response-&amp;gt;get_cdata( ).    &lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 05 Aug 2024 03:57:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/abap-integration-with-fastapi-for-file-uploads-seeking-code-guidance/qaa-p/13782535#M4839820</guid>
      <dc:creator>naveenkumarb142</dc:creator>
      <dc:date>2024-08-05T03:57:55Z</dc:date>
    </item>
  </channel>
</rss>

