【轉載】SAP中用json資料格式呼叫http介面傳送簡訊郵件案例
使用JSON 資料傳輸的可以參考:
在系統 VL02N 交貨過賬後觸發傳送郵件、簡訊的功能,簡訊郵件介面是經過第三方系統封裝,然後開發出的API介面。
請求地址: http://180.167.01.01/ns/sends
請求方式: POST
引數格式: JSON
請求引數:
- {
- "ncode": "sapOrderSend",
- "source": "訂單號",
- "contacts": [
- {
- "code": "訂貨人郵箱地址",
- "type": "1"
- },
- {
- "code": "訂貨人手機號",
- "type": "2"
- }
- ],
- "params": {
- "contactName": "訂貨人姓名",
- "contactCode": "訂貨人編號",
- "code": "訂單號",
- "createDate": "訂單建立時間",
- "storageName": "發貨倉庫名稱",
- "shipTypeName": "發貨方式名稱"
- }
- }
複製程式碼
RFC介面程式碼:
- 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.
相關文章
- 郵件和簡訊傳送
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- 利用Oracle資料庫傳送郵件Oracle資料庫
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- 郵件傳送
- 傳送郵件
- java郵件傳送Java
- python傳送郵件Python
- Django——郵件傳送Django
- gmail傳送郵件AI
- phpcms傳送郵件PHP
- 郵件的傳送
- Laravel 傳送郵件Laravel
- thinkjs 傳送郵件JS
- SpringBoot傳送郵件Spring Boot
- SpringBoot郵件傳送Spring Boot
- Laravel傳送郵件Laravel
- PHP傳送郵件PHP
- nodejs 傳送郵件NodeJS
- Laravel 使用 rabbitmq 非同步傳送郵件小案例.LaravelMQ非同步
- SpringBoot開發案例之郵件多例項傳送Spring Boot
- JAVA 資料寫入excel併傳送郵件JavaExcel
- 【轉載】傳送SAP附件到 WEBSERVICE介面(二進位制)Web
- JavaMail:java使用QQ郵箱傳送郵件簡單版。JavaAI
- Laravel 佇列訊息與傳送郵件Laravel佇列
- golang傳送郵件(qq郵箱)Golang
- 郵件傳送案例流的形式檔案不落盤
- Android 快速傳送郵件Android
- 使用phpmailer傳送郵件PHPAI
- SpringBoot傳送郵件(二)Spring Boot
- SpringBoot傳送郵件(三)Spring Boot
- 定時傳送郵件
- 使用 smtplib 傳送郵件
- Python SMTP傳送郵件Python
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- SpringBoot整合郵件傳送Spring Boot
- Ubuntu的郵件傳送Ubuntu