SAP ABAP 调用HTTP使用JSON格式发送数据

2023-10-13 14:50:07 浏览数 (1)

请求地址: http://xxxxxx 请求方式: POST 参数格式: JSON 请求参数: 

代码语言:javascript复制
{
    "ncode": "sapOrderSend",
    "source": "订单号",
    "contacts": [
        {
            "code": "订货人邮箱地址",
            "type": "1"
        },
        {
            "code": "订货人手机号",
            "type": "2"
        }
    ],
    "params": {
        "contactName": "订货人姓名",
        "contactCode": "订货人编号",
        "code": "订单号",
        "createDate": "订单创建时间",
        "storageName": "发货仓库名称",
        "shipTypeName": "发货方式名称"
    }
}

RFC接口代码:

代码语言:javascript复制
FUNCTION zot_do_send_message.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_VBELN) TYPE  VBELN_VL OPTIONAL
*"  EXCEPTIONS
*"      URL_ERROR
*"----------------------------------------------------------------------
  DATA: lr_http_client TYPE REF TO if_http_client .
  DATA: lv_url TYPE string,
        lv_result TYPE string,
        lv_post_string TYPE string,
        lv_len TYPE i.

  TYPES:BEGIN OF ty_contacts,
          code TYPE string,
          type TYPE char1,
       END OF ty_contacts.
  TYPES:BEGIN OF ty_params,
          contactname  TYPE string,
          contactcode  TYPE string,
          code         TYPE string,
          createdate   TYPE string,
          storagename  TYPE string,
          shiptypename TYPE string,
      END OF ty_params.
  DATA:lt_contacts TYPE TABLE OF ty_contacts.
  TYPES:BEGIN OF ty_message,
          ncode TYPE string,
          source TYPE string,
          contacts LIKE lt_contacts,
          params TYPE ty_params,
        END OF ty_message.
  DATA:ls_message  TYPE ty_message.
  DATA:ls_contacts TYPE ty_contacts.
  DATA:BEGIN OF lt_vbap OCCURS 0,
        vbeln TYPE vbap-vbeln,
        posnr TYPE vbap-posnr,
        ktgrm TYPE vbap-ktgrm,
      END OF lt_vbap.

  DATA:lv_vbeln_so   TYPE vbak-vbeln,
       lv_tel_number TYPE adr2-tel_number,
       lv_smtp_addr  TYPE adr6-smtp_addr,
       lv_kunnr      TYPE kna1-kunnr,
       lv_name       TYPE kna1-name1,
       lv_erdat TYPE vbak-erdat,
       lv_bname TYPE vbak-bname,
       lv_bezei TYPE t173t-bezei,
       lv_lgobe TYPE t001l-lgobe.

  IF i_vbeln IS INITIAL.
    EXIT.
  ENDIF.

  SELECT SINGLE url INTO lv_url FROM zot_message WHERE fm_name = 'ZOT_DO_SEND_MESSAGE' AND pm1 = 'URL'.
  IF sy-subrc NE 0.
    MESSAGE '请在表ZOT_MESSAGE配置URL参数' TYPE 'E' RAISING url_error.
  ENDIF.

  SELECT vbap~vbeln
         vbap~posnr
         vbap~ktgrm
    INTO TABLE lt_vbap
    FROM lips
    INNER JOIN vbap ON lips~vgbel = vbap~vbeln AND vbap~abgru = ''
    INNER JOIN vbak ON vbap~vbeln = vbak~vbeln AND vbak~vkorg = '3110'  "国内发短信
    WHERE lips~vbeln = i_vbeln.
  DELETE lt_vbap WHERE ktgrm = '02'.  "技术服务
  IF lt_vbap[] IS INITIAL.
    EXIT.
  ENDIF.

  SELECT SINGLE lips~vgbel adr2~tel_number adr6~smtp_addr kna1~kunnr kna1~name1
  INTO (lv_vbeln_so,lv_tel_number,lv_smtp_addr,lv_kunnr,lv_name)
  FROM lips
  INNER JOIN vbpa ON lips~vgbel = vbpa~vbeln AND vbpa~posnr = space AND vbpa~parvw = 'WE'
  INNER JOIN kna1 ON vbpa~kunnr = kna1~kunnr
  LEFT JOIN adr2 ON kna1~adrnr = adr2~addrnumber AND adr2~persnumber = space AND adr2~r3_user = '3'
  LEFT JOIN adr6 ON kna1~adrnr = adr6~addrnumber AND adr6~persnumber = space
  WHERE lips~vbeln = i_vbeln.

  IF lv_tel_number IS INITIAL AND lv_smtp_addr IS INITIAL.
    EXIT.
  ENDIF.

  SELECT SINGLE
  likp~erdat
  vbak~bname
  t173t~bezei
  t001l~lgobe
  INTO (lv_erdat,lv_bname,lv_bezei,lv_lgobe)
  FROM likp
  INNER JOIN lips ON likp~vbeln = lips~vbeln
  INNER JOIN vbak ON lips~vgbel = vbak~vbeln
  LEFT JOIN t173t ON t173t~spras = sy-langu AND t173t~vsart = likp~vsart
  LEFT JOIN t001l ON lips~werks = t001l~werks AND lips~lgort = t001l~lgort
  WHERE likp~vbeln = i_vbeln.

*-->数据初始化成嵌套内表形式
  ls_message-ncode = 'sapOrderSend'.
  IF lv_bname IS NOT INITIAL.
    ls_message-source = lv_bname.
  ELSE.
    ls_message-source = i_vbeln.
  ENDIF.
  IF lv_smtp_addr IS NOT INITIAL.
    ls_contacts-code = lv_smtp_addr.
    ls_contacts-type = '1'.
    APPEND ls_contacts TO ls_message-contacts.
  ENDIF.
  IF lv_tel_number IS NOT INITIAL.
    ls_contacts-code = lv_tel_number.
    ls_contacts-type = '2'.
    APPEND ls_contacts TO ls_message-contacts.
  ENDIF.

  ls_message-params-contactname = lv_name.
  ls_message-params-contactcode = lv_kunnr.
  IF lv_bname IS NOT INITIAL.
    ls_message-params-code = lv_bname.
  ELSE.
    ls_message-params-code = lv_vbeln_so.
  ENDIF.

  IF lv_erdat IS NOT INITIAL.
    CONCATENATE lv_erdat 0(4) lv_erdat 4(2) lv_erdat 6(2) INTO ls_message-params-createdate SEPARATED BY '-'.
  ENDIF.
  ls_message-params-storagename = lv_lgobe.
  ls_message-params-shiptypename = lv_bezei.

*-->内表数据转-->json格式
  lv_post_string = /ui2/cl_json=>serialize( data = ls_message compress = ABAP_true pretty_name = abap_true ).

  REPLACE ALL OCCURRENCES OF 'contactname'  IN lv_post_string WITH 'contactName'.
  REPLACE ALL OCCURRENCES OF 'contactcode'  IN lv_post_string WITH 'contactCode'.
  REPLACE ALL OCCURRENCES OF 'createdate'   IN lv_post_string WITH 'createDate'.
  REPLACE ALL OCCURRENCES OF 'storagename'  IN lv_post_string WITH 'storageName'.
  REPLACE ALL OCCURRENCES OF 'shiptypename' IN lv_post_string WITH 'shipTypeName'.

*  lv_len = strlen( lv_post_string ) .
  lv_len = cl_abap_list_utilities=>dynamic_output_length( lv_post_string ).

*send
*  lv_url = 'http://192.168.01.01:8088/ns/sends'.
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = lv_url
    IMPORTING
      client             = lr_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'POST'.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = '~server_protocol'
      value = 'HTTP/1.1'.

  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'application/json; charset=utf-8'.
  CALL METHOD lr_http_client->request->set_header_field
    EXPORTING
      name  = 'Accept'
      value = 'application/json'.
*  CALL METHOD lr_http_client->request->set_header_field
*    EXPORTING
*      name  = 'Content-Length'
*      value = len_str.


  CALL METHOD lr_http_client->request->set_cdata
    EXPORTING
      data   = lv_post_string
      offset = 0
      length = lv_len.

  CALL METHOD lr_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  CALL METHOD lr_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

  lv_result = lr_http_client->response->get_cdata( ).

ENDFUNCTION.

0 人点赞