Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Uploading files in a Rest webservice using PLSQL

b337b730-2b8e-4e66-9973-b936a522ff76Mar 27 2020 — edited Mar 27 2020

Hi,

I've been searching extensively for days, but haven't found the solution yet.

My objective is to invoke a WS from PLSQL using UTL_HTTP, but the content is a file (image, or other) stored as BLOB field in a table.

I have no problems invoking other WSs in the normal JSON format an treating the output with PLSQL. The problem exists only when i want to upload a file.

The WS in question expects this:

Upload file to tickets:

Post to http://{IP/DNS}/upload/upload2tickets.php

Body parameters:

|

key

|

value

|

description

|
|

file

(required)

| 111.pdf | file to send |

If i use Postman, i am able to call it successfuly, choosing (in the body) form-data, selecting file, uploading the file and hitting "SEND":

pastedImage_0.png

However if i use RAW instead of form-data, with something like {"file":"C:/my_dir/imagem_01.jpg"} it doesn´t work.

I mean, the WS is executed without error, but the response is not what it should be.

In the PLSQL arena i've tried this (code snipet), but the result is the same, it executes but the result is not what it shoud be:

DECLARE

l_buffer RAW(32767);

l_amount INTEGER := 32767;

l_pos INTEGER := 1;

l_blob_len PLS_INTEGER;

BEGIN

/* p_file is the file to upload as a BLOB parameter */

IF p_file IS NOT NULL THEN

BEGIN

  l\_blob\_len := dbms\_lob.getlength(p\_file);

  utl\_http.set\_header(v\_req, 'Content-Type', 'application/octet-stream'); -- v\_req is a parameter

  utl\_http.set\_header(v\_req, 'Content-Length', l\_blob\_len);

  -- Read chunks of the BLOB and write the image into the Request

  WHILE l\_pos \< l\_blob\_len LOOP

    dbms\_lob.read(p\_file, l\_amount, l\_pos, l\_buffer);

    utl\_http.write\_raw(v\_req, l\_buffer);

    l\_pos := l\_pos + l\_amount;

  END LOOP;

EXCEPTION

  WHEN OTHERS THEN

    p\_err := SQLCODE;

    p\_msg := 'ERROR: ' || SQLERRM;

END;

END IF;

END;

Any help would be greatly appreciated.

Rodrigo Coutinho

Portugal

Comments
Post Details
Added on Mar 27 2020
2 comments
1,651 views