ABAP发送http post请求

在 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 data
  call function 'Z_BAPI_GLACCPERIODBALANCES'
    exporting
      companycode  = 'Z900'
      fiscalyear   = '2020'
      fiscalperiod = '10'
    tables
      acc_balances = gt_accbal.

  " 序列化
  create object gr_serializer
    exporting
      data   = 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_url
    exporting
      url                = lv_url
    importing
      client             = lo_http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.
  if sy-subrc <> 0.
    "lv_subrc = sy-subrc.
    message id sy-msgid type sy-msgty number sy-msgno with
               sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    exit.
  endif.

  " 设置content type和character set
  lo_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).

  " 设置方法为 post
  lo_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_cdata
    exporting
      data   = lv_json_str
      offset = 0
      length =  lv_len .

  " 发送请求
  lo_http_client->send(  exceptions http_communication_failure = 1
                                    http_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 = 1
                                      http_invalid_state         = 2
                                      http_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 json

app = 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 file
        with 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')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容