0

I am trying to call a restful API from my Oracle procedure. First, the API method is of type get and not post, so parameters are sent through header. the main purpose of the API is to send the received message as SMS to some providers and sometimes they are in Arabic format; We realized that Arabic received SMS are incomprehensible;

So I created a test procedure that takes a message and sends it to a test API method that returns the same message as response.

The API call succeeded but the response, only when arabic format is used, looks like ����. What should be added to my procedure so messages can be readable? I have tried to use escape for the message and to set the header format as you can see in below template, but unfortunately nothing succeeded:

PROCEDURE TEST(lang       VARCHAR2, 
               message    VARCHAR2,
               P_RESPONSE OUT VARCHAR2) AS

    v_request         UTL_HTTP.req;
    v_response        UTL_HTTP.resp;
    v_text            VARCHAR2(1024);
    v_url             VARCHAR2(1024);
    v_message         VARCHAR2(1024);
    l_webservice_link VARCHAR2(128); 


  BEGIN
    BEGIN

      P_RESPONSE := '';
     v_message         := utl_url.escape(message);
      --v_message         :=utl_url.escape(message,false,'UTF-8');
      --v_message         :=utl_url.escape(message,false,'windows-1256');
      --v_message         :=utl_url.escape(message,false,'AL32UTF8');
      --v_message         :=utl_url.escape(message,false,'AR8MSWIN1256');

      l_webservice_link := GET_PARAM('REST_API_URL');

      v_url             := l_webservice_link || 
                           'Mytest?strMessage=' || v_message|| 
                           '&strLang=' || lang;

      v_request         := UTL_HTTP.begin_request(v_url);

      --UTL_HTTP.set_header(v_request, 'Content-Type', 'charset=UTF-8');
      --UTL_HTTP.set_header(v_request, 'Content-Type', 'windows-1256');

      v_response := UTL_HTTP.get_response(v_request);

      LOOP
        BEGIN
          UTL_HTTP.read_text(v_response, v_text);
          DBMS_OUTPUT.put_line(v_text);

        EXCEPTION
          WHEN UTL_HTTP.end_of_body THEN
            NULL;
        END;

        EXIT WHEN v_text IS NULL;
      END LOOP;

      UTL_HTTP.end_response(v_response);

      IF v_response.status_code <> 200 THEN
       P_RESPONSE := v_response.reason_phrase; 
      END IF;

    EXCEPTION
      WHEN OTHERS THEN
        P_RESPONSE := 'An error has occured: ' || SQLERRM;        
    END;

  END TEST;

Any help is more than appreciated.

Mlle 116
  • 1,149
  • 4
  • 20
  • 53
  • when you are receiving the arabic characters, they are being corrupted ? is that your issue is that arabic character are wrong – Moudiz Jan 17 '19 at 09:47
  • @Moudiz yes, the v_text retrieved from v_response is corrupted – Mlle 116 Jan 17 '19 at 09:48
  • run this on the database `selecT * from nls_database_parameters where parameter='NLS_CHARACTERSET'` then check the nlslang on your pc are both have similar charset ? – Moudiz Jan 17 '19 at 09:50
  • @Moudiz `NLS_CHARACTERSET.value = AR8ASMO8X` ; `NLS_LANG value = ARABIC_AMERICA.AL32UTF8` – Mlle 116 Jan 17 '19 at 09:51
  • how about the nlslang ? go to run>regedit>HKEY_LOCAL_MACHINE > software>ORACLE>KET_ORACLIENT then inside of it nlslang – Moudiz Jan 17 '19 at 09:53
  • @Moudiz so you suggest to have similar charset on pc and database? – Mlle 116 Jan 17 '19 at 10:15
  • Do you get Arabic characters if you execute a simple `select 'اَللُّغَةُ اَلْعَرَبِيَّة' from dual;` – Wernfried Domscheit Jan 17 '19 at 11:37
  • @WernfriedDomscheit Yes – Mlle 116 Jan 17 '19 at 11:44
  • @H.Al well ofcourse the nlslang should be similar to the database charset, The steps I usually do concerning foriegn language , first I try to insert an arabic word in a column, then I try to select this column and I get correct display. are you able to do such steps ? insert arabic word in a column then get its correct display ? – Moudiz Jan 17 '19 at 12:04
  • @Moudiz, `NLS_LANG` seems to be fine as `select 'اَللُّغَةُ اَلْعَرَبِيَّة' from dual;` works properly. – Wernfried Domscheit Jan 17 '19 at 14:19
  • What do you get from `select DUMP(v_text, 1016) from dual;`? Maybe use `substr(v_text, 1, 10)` rather than `v_text` if the text is too big. – Wernfried Domscheit Jan 17 '19 at 14:22

1 Answers1

0

Try to insert this code:

Charset VARCHAR2(20);

BEGIN

    SELECT UTL_I18N.MAP_CHARSET(VALUE)
    INTO Charset
    FROM nls_database_parameters
    WHERE parameter = 'NLS_CHARACTERSET';

UTL_HTTP.set_header(v_request, 'Content-Type', 'text/html; charset='||Charset);

I am not familiar with REST, I don't know wether text/html; is required and correct.

Update

I just see your database character set it AR8ASMO8X which does not have any IANA name (at least not according to Oracle UTL_I18N.MAP_CHARSET)

In this case try

UTL_HTTP.set_header(v_request, 'Content-Type', 'text/html; charset=UTF-8');
UTL_HTTP.begin_request(CONVERT(v_url,'AL32UTF8'));

Most likely the server returns response in UTF-8 - would be the most common one, otherwise check the header of the response.

Then try this:

UTL_HTTP.SET_BODY_CHARSET(v_response, 'AL32UTF8');

Apart from all above you may have also a display issue, i.e. inside Oracle everything would be fine, just your client is not able to display the characters properly, see OdbcConnection returning Chinese Characters as "?"

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • First thank you for your help , but unfortunately the suggested solutions above did not work; But the returned message changed from ���� to ??? . Concerning the first suggestion (select in charset) --> the returned value is empty, and whenever I try to call the AP it fails and returns an oracle error ORA-01482 – Mlle 116 Jan 17 '19 at 10:19
  • Concerning the second suggestion: it throws an error, I reversed the lines and set v_request = begin_request to fix the issue ... API was called successfully, english messages are readble but arabic are returned as ??? – Mlle 116 Jan 17 '19 at 10:21
  • Concerning the third suggestion, I kept the changes of suggestion 2 , and added the new suggestion 3 , but nothing has changed. Please note that, I don't think it's a display issue because I already called another API SOAP and I am sending arabic messages successfully using charset windows-1256, which is not working here. – Mlle 116 Jan 17 '19 at 10:24
  • Windows-1256 does not support Arabic characters, so that's clear why it does not work. – Wernfried Domscheit Jan 17 '19 at 14:18