SAP ABAP 发送/调用HTTP POST/GET请求的通用方法

2023-10-13 14:29:23 浏览数 (3)

使用类if_http_client基本可以解决大多数ABAP调用http服务的问题。在调用服务前可以先用postman来测试服务,确定可输入的参数。

通用代码如下:

代码语言:javascript复制
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

再举一个例子

以post方式为例,request的参数都是通过body来传输的。正常服务发起比较简单,通过if_http_client->request->set_data设置传输值,if_http_client->request->set_header_field设置header的content-type。如果服务端对body的数据字符集有要求,比如gbk,则需要先将body数据进行jbk转码,然后在if_http_client->request->set_content_type中设置content-type,示例代码如下:

代码语言:javascript复制
  "设定传输请求内容格式以及编码格式
  lo_http_client->request->set_content_type( content_type = 'application/XML; charset=GBK' ).

  "设定调用服务
  lo_http_client->request->set_method( if_http_request=>co_request_method_post ).

  CLEAR:lv_result_xstring.
  lv_result_xstring = cl_abap_codepage=>convert_to(
        source      =  uv_reqxml
        codepage    = 'GBK'
                          ).
  lv_len = xstrlen( lv_result_xstring ).
  lo_http_client->request->set_data(
     EXPORTING
       data   = lv_result_xstring
       offset = 0
       length = lv_len ).


  lo_http_client->send(
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2 ).

  lo_http_client->receive(
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3 ).


*  lv_result_string = lo_http_client->response->get_cdata( ).

  CLEAR:lv_result_xstring,lv_result_string.
  lv_result_xstring = lo_http_client->response->get_data( ).
  lv_result_string  = cl_abap_codepage=>convert_from(
        source      =  lv_result_xstring
        codepage    = 'GBK' ).

0 人点赞