在 SAP 常用的接口技术中,大多是外部系统主动请求,比如外部系统调用 RFC 函数,如果需要 SAP 侧主动推送数据,RFC 则不能实现,需要 iDoc 之类的技术才行。本文介绍在 ABAP 中提交 http post 请求的方式,实现 SAP 侧主动推送数据,外部只需要有 web server,能接收到数据就行。这种方法比较简单,而且也没有编程语言的限制。

本例实现的功能:ABAP 调用 Z_BAPI_GLACCPERIODBALANCES 自定义函数 (z 函数的代码在之前的博文中有提供),把数据从 internal table 转换为 json 格式的字符串,发送到 web server,并接收服务器返回的信息。

ABAP 发送 http post 请求

report  z_http_post." data to post
data: gt_accbal like standard table of zglaccbalance with header line.
data: lv_json_str type string. "发送报文
data: gr_serializer type ref to zcl_trex_json_serializer.start-of-selection." get datacall function 'Z_BAPI_GLACCPERIODBALANCES'exportingcompanycode  = 'Z900'fiscalyear   = '2020'fiscalperiod = '10'tablesacc_balances = gt_accbal." 序列化create object gr_serializerexportingdata   = gt_accbal[] .call method gr_serializer->serialize( ) .lv_json_str = gr_serializer->get_data( )." 发送http post请求perform http_post.*&---------------------------------------------------------------------*
*&      Form  HTTP_POST
*&---------------------------------------------------------------------*
form http_post .data: lv_url type string.     "http 服务接口地址data: lo_http_client  type ref to if_http_client.data: lv_len type i."发送报文长度data: lv_resp type string.data: lv_message  type string.data: lv_mtype    type bapi_mtype.data: lv_code     type sysubrc." 设置http接口地址lv_url = 'http://192.168.3.14:5000/testpost/'."创建客户端请求call method cl_http_client=>create_by_urlexportingurl                = lv_urlimportingclient             = lo_http_clientexceptionsargument_not_found = 1plugin_not_active  = 2internal_error     = 3others             = 4.if sy-subrc <> 0."lv_subrc = sy-subrc.message id sy-msgid type sy-msgty number sy-msgno withsy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.exit.endif." 设置content type和character setlo_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' )." 设置方法为 postlo_http_client->request->set_method( if_http_request=>co_request_method_post )." 设置待传输内容lv_len = strlen( lv_json_str ).call method lo_http_client->request->set_cdataexportingdata   = lv_json_stroffset = 0length =  lv_len ." 发送请求lo_http_client->send(  exceptions http_communication_failure = 1http_invalid_state         = 2 ).if sy-subrc <> 0."操作失败,获取失败原因lo_http_client->get_last_error( importing message = lv_message code = lv_code ).lv_mtype = 'E'.exit.endif." 读取远程服务返回的结果消息。lo_http_client->receive( exceptions http_communication_failure = 1http_invalid_state         = 2http_processing_failed     = 3 ).if sy-subrc <> 0 ." lv_subrc = sy-subrc.lo_http_client->get_last_error( importing message = lv_message code = lv_code  ).lv_mtype = 'E'.write: lv_message, lv_code.exit.else." 读取返回返回内容clear lv_resp.lv_resp = lo_http_client->response->get_cdata( ).endif.write:lv_resp.
*  MESSAGE LV_MESSAGE TYPE LV_MTYPE .
endform.                    "HTTP_POST

web server 接收 json 消息并下载为本地文件

使用 python 的 flask 框架来实现。

from flask import Flask, jsonify, request
import jsonapp = Flask(__name__)@app.route("/")
def index():return "Index page"@app.route("/testpost/", methods = ["GET","POST"])
def process_http_post():if request.method == "POST":data = request.get_json()print(data)# write data to filewith open('output.txt', mode='w', encoding='utf-8') as f:json.dump(data, f, ensure_ascii=False)return str(len(data)) + ' lines of data was received from SAP successfully.'if __name__ == "__main__":app.run(host='0.0.0.0')

ABAP发送http post请求相关推荐

  1. postman无法获得响应_【原创翻译】POSTMAN从入门到精通系列(二):发送第一个请求...

    通过API请求,您可以与具有要访问的API端点的服务器联系,并执行某些操作.这些操作是HTTP方法. 最常用的方法是GET,POST,PUT和DELETE.方法的名称是不言自明的.例如,GET使您可以 ...

  2. ajax 跨域请求,每次会发送两个请求?

    2019独角兽企业重金招聘Python工程师标准>>> 跨域已经是个老话题了,但是最近搞百度的语音接口的时候,在服务端配置了 CORS ,跨域倒是没问题,但是每次都会发送两个请求: ...

  3. python同时同步发送多个请求_python如何实现“发送一个请求,等待多个响应”的同步?...

    我正在写一些代码通过串行口与单片机通信. MCU端基本上是一个请求/响应服务器. 一个或多个MCU发送我的请求. 然而,响应可以异步到达并且具有随机延迟,但是响应的顺序将保持不变. 另外,我的应用程序 ...

  4. 调用webapi 错误:使用 HTTP 谓词 POST 向虚拟目录发送了一个请求,而默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件。的解决方案

    调用webapi 错误:使用 HTTP 谓词 POST 向虚拟目录发送了一个请求,而默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件.的解决方案 参考文章: (1)调用weba ...

  5. 解决python发送multipart/form-data请求上传文件的问题

    解决python发送multipart/form-data请求上传文件的问题 参考文章: (1)解决python发送multipart/form-data请求上传文件的问题 (2)https://ww ...

  6. 如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查

    我们使用Java的RestTemplate或者Apache的HTTPClient编程的时候,经常遇到需要跟踪Java 代码发送的HTTP请求明细的情况.和javascript代码在浏览器里发送请求可以 ...

  7. easyui数据请求两个url_jQuery Easyui datagrid连续发送两次请求问题

    XXXXXX.datagrid({ url: "${pageContext.request.contextPath}/xx/xx/xx, }); 用上述方式动态加载datagrid的数据时, ...

  8. 循环发ajax请求,在循环中发送jquery ajax请求

    我在我的Web应用程序的Map中实现了"空间选择",它选择Streets的数量.选择后,我会为使用"选择处理程序"选择的所有街道获取唯一的街道标识.在循环中发送 ...

  9. react发送和接收请求_React行为编程简介:请求,等待和阻止

    react发送和接收请求 by Luca Matteis 卢卡·马蒂斯(Luca Matteis) React行为编程简介:请求,等待和阻止 (An intro to Behavioral Progr ...

最新文章

  1. 初学Java的那段日子
  2. 为什么你的提问没人解答?
  3. 你真的了解CSS3硬件加速吗?
  4. 使用AutoMake轻松生成Makefile
  5. Java并发编程-ReentrantLock源码分析
  6. ArcGIS实验教程——实验二十二:空间数据符号化
  7. hive 创建访问用户_hive创建角色并赋权
  8. 共阳数码管段码表_简单共阴极数码管电路图大全
  9. c语言leg 10,Leg massaging device
  10. 使用matplotlib绘制K线图以及和成交量的组合图
  11. 海量图标矢量图免费下载【来自阿里】
  12. 什么是UE设计?UI设计又是什么?UE和UI有什么区别?
  13. 手机计算机藏应用,手机“计算器”隐藏功能,一键把隐私照片加密
  14. 在HTML中lt;是什么意思?
  15. 安排软件保护服务在 2022-07-26T23:00:43Z 时重新启动成功。原因: RulesEngine
  16. 谷粒商城-07-p102-p138
  17. linux scp 限制速度,scp和rsyc限速传输
  18. 创建临时表空间组+查询临时表空间组+临时表空间移动到别的临时表空间组中
  19. int a = 200,300,强制转换成byte会是多少?
  20. vscode设置编码格式的几种方法

热门文章

  1. Linux 网卡驱动sk_buff内核源码随笔
  2. HTML标签hideFocus的功能
  3. cool-admin框架后端使用-node版本,使用事务装饰器来创建和事务回滚
  4. mac m4a转mp3怎么转?
  5. linux 编程中忽略SIGPIPE信号
  6. 【高数】微分中值定理有关的一道证明题
  7. 三探String类型
  8. 服务器刷怪塔制作方法,迷你世界制作刷怪塔须知 制作刷怪塔需要注意些什么...
  9. 低粉号、带货难?破解快手新晋达人4个爆单套路
  10. cocoa touch——UIControl——state,highlighted,enabled,selected