SAP调用RestfulApi接口POST数据到外部系统
作者:明光烁亮
出处:http://www.cnblogs.com/hezhongxun/
微信号:MMGGSSLL330 欢迎加好友一起交流SAP! 视频资料共享。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
之前只写了get外部接口的数据,今天刚好做了个post的接口就随便记录下。
详细的业务就不多讲了,直接上方法和代码吧。
外部已经提供了一个post地址,而且要求参数也添加到地址后面,地址如下:
'http://192.168.0.XX:XXX/XXX/fileAction.do?funid=erp_ws&eventcode=syncData&nousercheck=1&user_id=SAP&type=2&data='
数据是以json的方式写入地址的。
使用RESTFUL接口post数据的方法顺序是:
1.创建http服务
2.设置http request的参数
3.把数据转换成json
4.发送http请求
5.接收返回的数据。
代码如下:
FUNCTION z_wms_delete_dn. *"---------------------------------------------------------------------- *"*"本地接口: *" EXPORTING *" VALUE(RETURN) TYPE ZSTRU_WMS_RETURN *" TABLES *" VLDATA STRUCTURE ZTSD002 OPTIONAL *"---------------------------------------------------------------------- DATA: lc_http_client TYPE REF TO if_http_client, l_url TYPE string, l_json_data TYPE string, l_result_string TYPE string, l_result TYPE zstru_wms_return. DATA: BEGIN OF ls_vldata. INCLUDE STRUCTURE ztsd002. DATA: loekz TYPE char1, END OF ls_vldata. DATA: lt_vldata LIKE TABLE OF ls_vldata. DATA lc_json TYPE REF TO cl_fdt_json_serializer. "json格式转换 DATA: lt_zwms_dn_delete TYPE TABLE OF zwms_dn_delete."记录数据 INCLUDE fbgenmac. fbgenmac 'Z_WMS_DELETE_DN'. LOOP AT vldata. ls_vldata-vbeln = vldata-vbeln. ls_vldata-posnr = vldata-posnr. ls_vldata-loekz = 'L'. APPEND ls_vldata TO lt_vldata. CLEAR: ls_vldata. APPEND INITIAL LINE TO lt_zwms_dn_delete ASSIGNING FIELD-SYMBOL(<fs_wms>). <fs_wms>-vbeln = vldata-vbeln. <fs_wms>-posnr = vldata-posnr. <fs_wms>-erdat = sy-datum. <fs_wms>-erzet = sy-uzeit. ENDLOOP. CASE sy-mandt. WHEN '100'. l_url = 'http://xxx.xxx.xxx.xxx/xx/fileAction.do?funid=erp_ws' && '&eventcode=syncData&nousercheck=1&user_id=SAP&type=2&data='. WHEN '800'. l_url = 'http://xxx.xxx.xxx.xxx/xx/fileAction.do?funid=erp_ws' && '&eventcode=syncData&nousercheck=1&user_id=SAP&type=2&data='. ENDCASE. *****转换json格式 CREATE OBJECT lc_json. CALL METHOD lc_json->serialize_int EXPORTING data = lt_vldata RECEIVING r_json = l_json_data. TRANSLATE l_json_data TO LOWER CASE.
CONCATENATE l_url l_json_data INTO l_url. *****创建http服务 CALL METHOD cl_http_client=>create_by_url EXPORTING url = l_url IMPORTING client = lc_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. IF sy-subrc <> 0. EXIT. ENDIF. CALL METHOD lc_http_client->request->set_header_field EXPORTING name = 'Content-Type' value = 'application/JSON; charset=utf-8'. CALL METHOD lc_http_client->request->set_method( 'POST' ). *****发送http服务 CALL METHOD lc_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4 OTHERS = 5. IF sy-subrc <> 0. EXIT. ENDIF. *****接收返回数据 CALL METHOD lc_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 4. l_result_string = lc_http_client->response->get_cdata( ).
*****解析JSON数据 CALL METHOD lc_json->deserialize_int EXPORTING json = l_result_string CHANGING data = l_result. return = l_result.
****最后我只是记录到自建表 LOOP AT lt_zwms_dn_delete ASSIGNING FIELD-SYMBOL(<fs_dn>). <fs_dn>-success = l_result-success. <fs_dn>-message = l_result-message. ENDLOOP. MODIFY zwms_dn_delete FROM TABLE lt_zwms_dn_delete. IF sy-subrc = 0. COMMIT WORK. ELSE. ROLLBACK WORK. ENDIF. ENDFUNCTION.
其实HTTP还是挺复杂的,只是sap都封装了很多方法,以后有时间可以研究下http方面的知识。
作者:明光烁亮
出处:http://www.cnblogs.com/hezhongxun/
微信号:MMGGSSLL330 欢迎加好友一起交流SAP! 视频资料共享。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。