<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ABAP HTTP body Chinese characters garbled code in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568458#M22055</link>
    <description>&lt;P&gt;Thanks. "filename" header field doesn't sound standard at all, but if the provider understands it, then it's fine.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Mar 2018 06:23:03 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2018-03-29T06:23:03Z</dc:date>
    <item>
      <title>ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568451#M22048</link>
      <description>&lt;P&gt;Hi all experts,&lt;/P&gt;
  &lt;P&gt;I need to upload a file by HTTP Post with content type multipart/form-data. All is well, but if the file name is chinese, it will become garbled code.&lt;/P&gt;
  &lt;P&gt;The screenshot below is the main code how to set the file name &lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/78603-maincode.jpg" /&gt;&lt;/P&gt;
  &lt;P&gt;I used Tool Wireshark to catch the HTTP stream, in the Wireshark you can find the file name become garbled code ‘###’&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/78602-wireshark-http-body-data.jpg" /&gt;&lt;/P&gt;
  &lt;P&gt;The follow the code &lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;FORM frm_upload_file  USING pv_token TYPE string

                             pv_path TYPE string

                             pv_fname TYPE string

                   CHANGING  cv_flg TYPE c

                             cv_msg TYPE string.

  DATA: lv_error_msg TYPE string,

        lv_url  TYPE string,

        lv_body TYPE string,

        lv_asjson TYPE xstring,

        lv_str TYPE string.



  DATA: lo_http_client TYPE REF TO cl_http_client,

        lo_http_request TYPE REF TO cl_http_request,

        li_http_client  TYPE REF TO if_http_client.



  DATA: lv_filename     TYPE string,

        lv_filelength   TYPE i,        "上次文件长度

        lv_header       TYPE xstring,

        lv_length       TYPE i.        "转成xstring后的长度



  TYPES: BEGIN OF lty_xml,

          c(400) TYPE x,

         END OF lty_xml.



  DATA: lt_xml TYPE TABLE OF lty_xml,

        lv_xml_target TYPE xstring.



  DATA: lv_code TYPE string,

        lv_response_msg TYPE string.



  cl_gui_frontend_services=&amp;gt;gui_upload(

    EXPORTING

      filename                = pv_fname

      filetype                = 'BIN'

  IMPORTING

      filelength              = lv_filelength

      header                  = lv_header

    CHANGING

      data_tab                = lt_xml

   EXCEPTIONS

      file_open_error         = 1

      file_read_error         = 2

      no_batch                = 3

      gui_refuse_filetransfer = 4

      invalid_type            = 5

      no_authority            = 6

      unknown_error           = 7

      bad_data_format         = 8

      header_not_allowed      = 9

      separator_not_allowed   = 10

      header_too_long         = 11

      unknown_dp_error        = 12

      access_denied           = 13

      dp_out_of_memory        = 14

      disk_full               = 15

      dp_timeout              = 16

      not_supported_by_gui    = 17

      error_no_gui            = 18

         ).

  IF sy-subrc &amp;lt;&amp;gt; 0.

    cv_flg = 'E'.

    cv_msg = '上传文件失败'.

    RETURN.

  ENDIF.



  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'

    EXPORTING

      input_length = lv_filelength

    IMPORTING

      buffer       = lv_xml_target

    TABLES

      binary_tab   = lt_xml.





  lv_length = xstrlen( lv_xml_target ).







  CONCATENATE 'http://' gv_ip '/fs/v1/upload/' INTO lv_url.

  CONCATENATE lv_url '?access_token=' pv_token  INTO lv_url.





  CALL METHOD cl_http_client=&amp;gt;create_by_url

    EXPORTING

      url                = lv_url

    IMPORTING

      client             = li_http_client

    EXCEPTIONS

      argument_not_found = 1

      plugin_not_active  = 2

      internal_error     = 3

      OTHERS             = 4.

  IF sy-subrc &amp;lt;&amp;gt; 0.

    cv_flg = 'E'.

    cv_msg = '创建url失败'.

    RETURN.

  ENDIF.



*set http method POST

  CALL METHOD li_http_client-&amp;gt;request-&amp;gt;set_method(

    if_http_request=&amp;gt;co_request_method_post ).





*set protocol version

  li_http_client-&amp;gt;request-&amp;gt;set_version(

   if_http_request=&amp;gt;co_protocol_version_1_1 ).



*content type

  CALL METHOD li_http_client-&amp;gt;request-&amp;gt;set_content_type

    EXPORTING

      content_type = 'multipart/form-data'.



  CLEAR lv_str.



  CONCATENATE 'access_token=' pv_token INTO lv_str.

  CALL METHOD li_http_client-&amp;gt;request-&amp;gt;if_http_entity~set_header_field

    EXPORTING

      name  = 'cookie'

      value = lv_str.



  CALL METHOD li_http_client-&amp;gt;request-&amp;gt;if_http_entity~set_header_field

    EXPORTING

      name  = 'Accept'

      value = 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'.



  CALL METHOD li_http_client-&amp;gt;request-&amp;gt;if_http_entity~set_header_field

    EXPORTING

      name  = 'Content-Type'

      value = 'multipart/form-data;charset=utf-8'.



**form  body

  DATA: it_formulario TYPE tihttpnvp,

        wa_formulario LIKE LINE OF it_formulario,

        part TYPE REF TO if_http_entity.



  "path

  part = li_http_client-&amp;gt;request-&amp;gt;if_http_entity~add_multipart( ).

  CALL METHOD part-&amp;gt;set_header_field

    EXPORTING

      name  = 'content-disposition'

      value = 'form-data;name="path"'.



  CALL METHOD part-&amp;gt;append_cdata

    EXPORTING

      data = pv_path.



  "filelen

  part = li_http_client-&amp;gt;request-&amp;gt;if_http_entity~add_multipart( ).

  CALL METHOD part-&amp;gt;set_header_field

    EXPORTING

      name  = 'content-disposition'

      value = 'form-data;name="filelen"'.



  lv_str = lv_length.

  CONDENSE lv_str NO-GAPS.

  CALL METHOD part-&amp;gt;append_cdata

    EXPORTING

      data = lv_str.

  .



  "filename

  CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'

    EXPORTING

      full_name     = pv_fname

    IMPORTING

      stripped_name = lv_filename

*     FILE_PATH     =

    EXCEPTIONS

      x_error       = 1

      OTHERS        = 2.

  IF sy-subrc &amp;lt;&amp;gt; 0.

    RETURN.

* Implement suitable error handling here

  ENDIF.



  CLEAR lv_str.

  CONCATENATE 'form-data;Content-Type:application/octet-stream;charset=utf-8;name="uploadfile"; filename="' lv_filename '"' INTO lv_str.



  part = li_http_client-&amp;gt;request-&amp;gt;if_http_entity~add_multipart( ).

  CALL METHOD part-&amp;gt;set_header_field

    EXPORTING

      name  = 'content-disposition'

      value = lv_str.

*      value = lv_xstr.



  CALL METHOD part-&amp;gt;append_data

    EXPORTING

      data = lv_xml_target.



  CALL METHOD part-&amp;gt;set_content_type

    EXPORTING

      content_type = 'text/html; charset=utf-8'.



  CALL METHOD li_http_client-&amp;gt;send

    EXPORTING

      timeout                    = 200

    EXCEPTIONS

      http_communication_failure = 1

      http_invalid_state         = 2

      http_processing_failed     = 3

      OTHERS                     = 4.

  IF sy-subrc NE 0.

    li_http_client-&amp;gt;get_last_error( IMPORTING message = lv_error_msg ).

    cv_flg = 'E'.

    cv_msg = lv_error_msg.

    RETURN.

  ENDIF.



* Receive the Response Object

  li_http_client-&amp;gt;receive( EXCEPTIONS http_communication_failure = 1

                                   http_invalid_state         = 2

                                   http_processing_failed     = 3 ).

  IF sy-subrc &amp;lt;&amp;gt; 0 .

    li_http_client-&amp;gt;get_last_error( IMPORTING message = lv_error_msg ).

    cv_flg = 'E'.

    cv_msg = lv_error_msg.

    RETURN.

  ENDIF.



  lv_body =  li_http_client-&amp;gt;response-&amp;gt;get_cdata( ).

  li_http_client-&amp;gt;close( ).



  CLEAR lv_asjson.

  PERFORM frm_json_names_to_upper USING lv_body lv_asjson.



  CALL TRANSFORMATION id SOURCE XML lv_asjson RESULT code = lv_code message = lv_response_msg.



  IF lv_code = '0'.

    cv_flg = 'S'.

    cv_msg = '上传成功'.

  ELSE.

    cv_flg = 'E'.

    cv_msg = lv_code &amp;amp;&amp;amp; ':' &amp;amp;&amp;amp; lv_response_msg.

  ENDIF.

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Oct 2017 07:52:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568451#M22048</guid>
      <dc:creator>former_member526351</dc:creator>
      <dc:date>2017-10-20T07:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568452#M22049</link>
      <description>&lt;P&gt;Hi Guozheng,&lt;/P&gt;
  &lt;P&gt;As it's a big hassle to build a mockup of your problem (even impossible as I don't know the whole process and business need) all I can offer are 3 suggestions:&lt;/P&gt;
  &lt;OL&gt;
   &lt;LI&gt;Try to use some other type of encoding for the code page (e.g. utf-16).&lt;/LI&gt;
   &lt;LI&gt;Try changing the Hex representation of the file name (the problem could be when uploading the file in the &lt;A href="https://archive.sap.com/discussions/thread/1180197"&gt; gui_upload&lt;/A&gt;).&lt;/LI&gt;
   &lt;LI&gt;A workaround - for example, map your file name into some unique numerator. The mapping shold be accessible on the receiving end (like a common DB table, XML attribute you can successfully transmit with the file etc.) and pass that numerator in the HTTP request and decode it back to its name on the receiving end.&lt;/LI&gt;
  &lt;/OL&gt;</description>
      <pubDate>Sat, 13 Jan 2018 13:32:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568452#M22049</guid>
      <dc:creator>iftah_peretz</dc:creator>
      <dc:date>2018-01-13T13:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568453#M22050</link>
      <description>&lt;P&gt;I think, method &lt;EM&gt;set_header_field&lt;/EM&gt; is not smart enough to convert your 3? character chinese filename into UTF-8. &lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;part = li_http_client-&amp;gt;request-&amp;gt;if_http_entity~add_multipart( ).
  CALL METHOD part-&amp;gt;set_header_field
    EXPORTING
      name  = 'content-disposition'
      value = lv_str.
*      value = lv_xstr&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Therefore it is shown as ### in Whireshark (looks like a UTF-16 into plain ASCII conversion).&lt;/P&gt;
  &lt;P&gt;Try to convert lv_filename into UTF-8, before you add it as header field.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;CONCATENATE 'form-data;Content-Type:application/octet-stream;charset=utf-8;name="uploadfile"; filename="' lv_filename '"' INTO lv_str.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Jan 2018 17:00:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568453#M22050</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-01-13T17:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568454#M22051</link>
      <description>&lt;P&gt;Difficult to find the solution, but this answer can be used as a starting point: &lt;A href="https://stackoverflow.com/a/35573100/9150270" target="test_blank"&gt;https://stackoverflow.com/a/35573100/9150270&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;HTTP encoding is explained in RFC 5987 (https://tools.ietf.org/html/rfc5987) : header fields may contain a value expressed in UTF-8 with URL escaping. Thus, 你好 (hello), which corresponds in UTF-8 to 6 bytes E4 BD A0 E5 A5 BD, which correspond to URL escaping %E4%BD%A0%E5%A5%BD, should be expressed:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;parmname*=UTF-8''%E4%BD%A0%E5%A5%BD&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Content-Disposition is explained in RFC 6266 (https://tools.ietf.org/html/rfc6266) : it says you may need to mention both filename and filename* if the receiver doesn't understand filename* (as it may sound impossible to translate a Chinese name into US-ASCII characters, choose any generic name).&lt;/P&gt;
  &lt;P&gt;Thus you should have (for hello):&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;Content-Disposition: form-data;Content-Type:application/octet-stream;charset=utf-8;name="uploadfile";filename*=UTF-8''%E4%BD%A0%E5%A5%BD;filename="file"&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jan 2018 20:18:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568454#M22051</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2018-01-13T20:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568455#M22052</link>
      <description>&lt;P&gt;With this solution from Sandra, ";charset=utf-8" becomes useless, because it has no influence on the parameter encoding.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;Content-Disposition:form-data;Content-Type:application/octet-stream;name="uploadfile";filename*=UTF-8''%E4%BD%A0%E5%A5%BD;filename="file"&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Jan 2018 22:27:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568455#M22052</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-01-13T22:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568456#M22053</link>
      <description>&lt;P&gt;In fact, char=utf-8 applies to the "part" body (section of the multipart HTTP), so it should not be confused with the encoding of the file name.&lt;/P&gt;
  &lt;P&gt;The code of the OP sends an XSTRING variable into the "part" body, which seems to be an XML, but impossible to know the character encoding as it's read from a file. So, the charset is either unknown or UTF-8, who knows.&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;U&gt;&lt;/U&gt;&lt;SUB&gt;&lt;/SUB&gt;&lt;SUP&gt;&lt;/SUP&gt;By the way, the content-type could also be text/xml...&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 09:29:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568456#M22053</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2018-01-14T09:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568457#M22054</link>
      <description>&lt;P&gt;Thank you for your answer.&lt;/P&gt;
  &lt;P&gt;I send a message to SAP Help Center, they said it was the problem of the third party vendor which can not process the Chinese characters. But I don't know why it will be OK if I use the Java code which was provided by the vendor.&lt;/P&gt;
  &lt;P&gt;Actually at last I contact our vendor who provided this HTTP server， they told me that I can set the filename in the head of the HTTP request also, then the problem was solved.&lt;/P&gt;
  &lt;P&gt;The following is the abap code&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;  "filename
  CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
    EXPORTING
      full_name     = pv_fname
    IMPORTING
      stripped_name = lv_filename
*     FILE_PATH     =
    EXCEPTIONS
      x_error       = 1
      OTHERS        = 2.
  IF sy-subrc &amp;lt;&amp;gt; 0.
    RETURN.
* Implement suitable error handling here
  ENDIF.

  CALL METHOD cl_http_utility=&amp;gt;if_http_utility~escape_url
    EXPORTING
      unescaped = lv_filename
*     options   =
    RECEIVING
      escaped   = lv_filename.


  CALL METHOD li_http_client-&amp;gt;request-&amp;gt;if_http_entity~set_header_field
    EXPORTING
      name  = 'filename'
      value = lv_filename.
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 02:44:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568457#M22054</guid>
      <dc:creator>former_member526351</dc:creator>
      <dc:date>2018-03-29T02:44:09Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP HTTP body Chinese characters garbled code</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568458#M22055</link>
      <description>&lt;P&gt;Thanks. "filename" header field doesn't sound standard at all, but if the provider understands it, then it's fine.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 06:23:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-http-body-chinese-characters-garbled-code/m-p/568458#M22055</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2018-03-29T06:23:03Z</dc:date>
    </item>
  </channel>
</rss>

