Oracle Apex REST response – PDF File not returning correctly

I have an REST API which take a PDF form and the data to fill this pdf form with. In response, pdf form with filled data is returned.

I have tested this API in Postman, work fine.

Now I am making a request with Apex to do the same, following is my code which is executed on page submit.

DECLARE
    lc_response     CLOB;
    l_blob          BLOB;
    lm_multipart    apex_web_service.t_multipart_parts;
    lc_json         CLOB := '[{
    "D1": 194645,
    "R1": 1,
    "R2": 9,
    "R3": 4,
    "R4": 6,
    "R5": 4,
    "R6": 5
    }]';
    lv_filename     VARCHAR2(20) := 'my_file.pdf';
    lv_base_url     VARCHAR2(100) := 'http://10.50.200.11:27005/report'; -- webservice base URL; 
    lv_api_key      VARCHAR2(100); -- API KEY 
    lb_blob         BLOB; -- Blob part of the multipart
    l_dest_offset   PLS_INTEGER := 1;
    l_src_offset    PLS_INTEGER := 1;
    l_lang_context  PLS_INTEGER := dbms_lob.default_lang_ctx;
    l_warning       PLS_INTEGER := dbms_lob.warn_inconvertible_char;
    l_file_mimetype apex_application_files.mime_type%TYPE;
BEGIN

    -- Select unfilled PDF template stored as BLOB in database
    SELECT
        input_template
    INTO lb_blob
    FROM
        python_input_template
    WHERE
        sno = 1;

    apex_web_service.set_request_headers(
                                        p_name_01  => 'x-api-key',
                                        p_value_01 => 'qwerty_key',
                                        p_name_02  => 'x-api-user',
                                        p_value_02 => 'APEX'
    );

    /*Supply data to fill the pdf with*/
    apex_web_service.append_to_multipart(
                                        p_multipart    => lm_multipart,
                                        p_name         => 'data_str',
                                        p_content_type => 'application/json',
                                        p_body         => lc_json
    );
    /*Supply the template to be filled*/
    apex_web_service.append_to_multipart(
                                        p_multipart    => lm_multipart,
                                        p_name         => 'template',
                                        p_filename     => lv_filename,
                                        p_content_type => 'application/pdf',
                                        p_body_blob    => lb_blob
    );

    lc_response := apex_web_service.make_rest_request(
                                                     p_url         => lv_base_url,
                                                     p_http_method => 'POST',
                                                     p_body_blob   => apex_web_service.generate_request_body(lm_multipart)
                   ); 

    /*convert CLOB responce to BLOB*/
    dbms_lob.createtemporary(
                            lob_loc => l_blob,
                            cache   => true
    );
    dbms_lob.converttoblob(
                          dest_lob     => l_blob,
                          src_clob     => lc_response,
                          amount       => dbms_lob.lobmaxsize,
                          dest_offset  => l_dest_offset,
                          src_offset   => l_src_offset,
                          blob_csid    => dbms_lob.default_csid,
                          lang_context => l_lang_context,
                          warning      => l_warning
    );

    sys.htp.init;
    owa_util.mime_header(
                        'application/pdf',
                        false,
                        'UTF-8'
    );
    sys.htp.p('Content-Disposition: attachment; filename="'
              || 'l_file_name.pdf'
              || '"');
    sys.htp.p('Content-length: '
              || sys.dbms_lob.getlength(l_blob));

    sys.owa_util.http_header_close;
    wpg_docload.download_file(l_blob);
    apex_application.stop_apex_engine;
END;

File is getting downloaded but in the file, Only the data that was filled is there, nothing else. So if My pdf template is like

Hi I am pdf template
Form start

Field A <form_field_to_be_filled>

Form End

Then I am only getting following in the generated file




<form_field_to_be_filled>


THIS IS ONLY HAPPENING WHEN MAKING REQUEST IN APEX, NOT IN POSTMAN.

Any ideas?

The APEX_WEB_SERVICE.MAKE_REST_REQUEST returns a CLOB, and for binary content, like a PDF, this can already corrupt data, which cannot be “healed” by converting back to a BLOB again.

You might use the MAKE_REST_REQUEST_B function, which returns a BLOB directly, then you can even drop your conversion calls.

l_blob := apex_web_service.make_rest_request_b(
              p_url         => lv_base_url,
              p_http_method => 'POST',
              p_body_blob   => apex_web_service.generate_request_body(lm_multipart) ); 

/* convert CLOB responce to BLOB - not needed any more

    dbms_lob.createtemporary(
                            lob_loc => l_blob,
                            cache   => true
    );
    dbms_lob.converttoblob(
                          dest_lob     => l_blob,
                          src_clob     => lc_response,
                          amount       => dbms_lob.lobmaxsize,
                          dest_offset  => l_dest_offset,
                          src_offset   => l_src_offset,
                          blob_csid    => dbms_lob.default_csid,
                          lang_context => l_lang_context,
                          warning      => l_warning
*/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật