2017-06-06 91 views
2

我試圖在PL/SQL中調用REST WebService,但它不起作用。我得到這個錯誤:如何使用PL/SQL中的表單數據和參數發送POST請求

Content-type must be multipart/form-data

以下是我有:

DECLARE 
    req UTL_HTTP.REQ; 
    resp UTL_HTTP.RESP; 
    value VARCHAR2(1024); -- URL to post to 
    v_url VARCHAR2(200) := 'http://local/api/ws'; 
    -- Post Parameters 
    v_param VARCHAR2(500) := 'art=11111\&qty=1'; 
    v_param_length NUMBER := length(v_param); 
BEGIN 
    req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST'); 
    UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0'); 
    UTL_HTTP.SET_HEADER (r  => req, 
         name => 'Content-Type', 
         value => 'multipart/form-data; charset=utf-8; boundary=/'); 
    UTL_HTTP.SET_HEADER (r  => req, 
         name => 'Content-Length', 
         value => v_param_length); 
    UTL_HTTP.WRITE_TEXT (r  => req, 
         data => v_param); 

    resp := UTL_HTTP.GET_RESPONSE(req); 
    LOOP 
    UTL_HTTP.READ_LINE(resp, value, TRUE); 
    DBMS_OUTPUT.PUT_LINE(value); 
    END LOOP; 
    UTL_HTTP.END_RESPONSE(resp); 
EXCEPTION 
    WHEN UTL_HTTP.END_OF_BODY THEN 
    UTL_HTTP.END_RESPONSE(resp); 
END; 

這裏是捲曲的例子:

curl -v -X POST -H "Content-Type: multipart/form-data" -F "art=11111" -F "qty=1" http://local/api/ws 

這個例子能正常工作,且捲曲,但我不知道爲什麼它不在PL/SQL中。 你能幫我嗎?

+0

相關,但沒有解決方案:https://stackoverflow.com/questions/23925278/oracle-utl-http-post-encoding-multipart-form-data –

回答

2

我不認爲你可以像使用URL一樣使用POST查詢來獲取查詢...

看起來你很難做到你期望的事情。下面是Ask Tom一些輸入:

的想法是生成後的文本,包括特定的「邊界」,這將是這樣的:

POST /path/to/script.php HTTP/1.0 
Host: example.com 
Content-type: multipart/form-data, boundary=AaB03x 
Content-Length: $requestlen 

--AaB03x 
content-disposition: form-data; name="field1" 

$field1 
--AaB03x 
content-disposition: form-data; name="field2" 

$field2 
--AaB03x 
content-disposition: form-data; name="userfile"; filename="$filename" 
Content-Type: $mimetype 
Content-Transfer-Encoding: binary 

$binarydata 
--AaB03x-- 

還有same issue developed這裏有很多的代碼。

希望它有幫助。

1

我用POST方法類似的過程在HTTP請求,但是你可以設置標題,如:

UTL_HTTP.set_header(v_http_request, 'Content-Type', 'application/x-www-form-urlencoded;charset=utf-8'); 

也要小心,如果網絡服務網站實現了安全證書,你的DBA必須創建一個錢包服務器端,並打開請求之前,你應該調用那個錢包:

utl_http.set_wallet('file:<your wallet file route>', '<your pass>'); 

更多關於錢包可以發現here

+0

Thx但WS只接受multipart/form-data – Sebus

相關問題