http - 使用 PL/SQL HTTP 上传 BLOB 文件

标签 http plsql oracle11g

通常,我使用以下结构来发送内容为 varchar2 和 numbers .. 等的 POST 请求

content := '{"Original File Name":"'||V_HOMEBANNER_1_EN_NAME(indx)||'"}';
  url := 'https://api.appery.io/rest/1/db/Names'; 
  req     := utl_http.begin_request(url, 'POST',' HTTP/1.1');
  UTL_HTTP.set_header(req, 'X-Appery-Database-Id', '5f2dac54b02cc6402dbe');
  utl_http.set_header(req, 'content-type', 'application/json');
  UTL_HTTP.set_header(req, 'X-Appery-Session-Token', sessionToken);
  utl_http.set_header(req, 'Content-Length', LENGTH(content));
  utl_http.write_text(req, content);
  res := utl_http.get_response(req);


BEGIN
  LOOP
    utl_http.read_line(res, buffer);
 END LOOP;
 utl_http.end_response(res);
EXCEPTION
WHEN utl_http.end_of_body THEN
  utl_http.end_response(res);
END;

而且它工作得很好。但是,现在我想将 blob 文件(jpg 图像)发送/上传到一些名为"file"的 MongoDB 集合中(所以 url := ttps://api.appery.io/rest/1/db/Files)。收藏指南将以下 cURL 作为一般建议:

curl -X POST \
  -H "X-Appery-Database-Id: 5f2dac54b02cc6402dbe" \
  -H "X-Appery-Session-Token: <session_token>" \
  -H "Content-Type: <content_type>" \
  --data-binary '<file_content>' \
  https://api.appery.io/rest/1/db/files/<file_name>

但我无法将此 cURL 转换为 PL/SQL 请求。具体来说,部分 (--data-binary '')

我在 Oracle 表中有这些 BLOB 文件,它们的存储名称如下:

+-----------+--------------+
| File_Name | File_content |
+-----------+--------------+
| PIC_1.jpg | BLOB         |
| PIC_2.jpg | BLOB         |
| PIC_3.jpg | BLOB         |
+-----------+--------------+

我的问题是,如何将这些图片上传到目标 URL 中?

最佳答案

受此启发blog在@JeffreyKemp 的建议下,我仅通过将 write_text() 替换为 write_raw() 来工作,以便将内容主体作为 BLOB(由 API 请求)发送。

以下代码是我的函数的关键部分,需要进行更改:

  content :=  V_HOMEBANNER_1_EN(indx);
  file_name := 'test.jpg';
  url  := 'https://api.appery.io/rest/1/db/files/'||file_name;      
  req     := utl_http.begin_request(url, 'POST',' HTTP/1.1');
  UTL_HTTP.set_header(req, 'X-Appery-Database-Id', '53fae4b02cc4021dbe');
  UTL_HTTP.set_header(req, 'X-Appery-Session-Token', sessionToken);
  utl_http.set_header(req, 'content-type', 'image/jpeg');
  req_length := DBMS_LOB.getlength(CONTENT);
  DBMS_OUTPUT.PUT_LINE(req_length);


  --IF MSG DATA UNDER 32KB LIMIT:
  IF req_length <= 32767
  THEN 
  begin
  utl_http.set_header(req, 'Content-Length', req_length);
  utl_http.write_raw(req, content);
  exception
  when others then
  DBMS_OUTPUT.PUT_LINE(sqlerrm);
  end;

  --IF MSG DATA MORE THAN 32KB
  ELSIF req_length >32767
  THEN
  BEGIN
  DBMS_OUTPUT.PUT_LINE(req_length);
  utl_http.set_header(req, 'Transfer-Encoding', 'Chunked');
  WHILE (offset < req_length)
  LOOP
  DBMS_LOB.read(content, amount, offset, buffer);
  utl_http.write_raw(req, buffer);
  offset := offset + amount;
  END LOOP;
  exception
  when others then
  DBMS_OUTPUT.PUT_LINE(sqlerrm);
  end;


  END IF;

  l_http_response := UTL_HTTP.get_response(req);
  UTL_HTTP.read_text(l_http_response, response_body, 32767);
  UTL_HTTP.end_response(l_http_response);

针对大于和小于 32KB 图像/jpg 进行了试验和测试。

关于http - 使用 PL/SQL HTTP 上传 BLOB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29226162/

相关文章:

ruby-on-rails - Rails 的页面缓存与 HTTP 反向代理缓存

java - 如何请求 CONNECT 与 Apache HttpComponents 客户端

ajax - 是什么引发了 HTTP 503 以及如何更改超时?

php - Guzzle HTTP : POST Request magically turns to GET request giving me "Method Not Allowed"

java - 从 Java 使用表类型参数调用 OracleDB 过程——DB 总是接收空值而不是值

sql - PLSQL 从字符串末尾开始显示数字

oracle - SQL Developer 剥离内联提示

oracle - PL/SQL PLS-00103 关于添加两个变量

performance - oracle 11g 中的查询错误

linux - 无法连接到 Linux Mint 中的 Oracle 11g XE Apex Web 界面