SAP ABAP调用REST服务

就是调用为外部HTTP接口 

zcl_json=>deserialize 因为版本问题  自定义的json转换函数  根据自己的版本使用对应函数就好

report ztest25.

data: url             type        string, "接口地址
      gv_json_in      type        string, "输入参数(账号密码啥的)
      json_data      type        string, "需要对接传输的数据
      http_client     type ref to if_http_client, "http客户端
      gv_accesstoken  type        string, "密钥
      apptoken_result type        string.

data l_sysubrc like sy-subrc. "错误信息
data l_error_text type string.
"获取的返回参数  字段根据实际自己的需求来
types:begin of gs_data2,
        access_token type string,
        success      type string,
        error_desc   type string,
        expire_time  type string,
        error_code   type string,
      end of gs_data2.
data:begin of gs_get_accesstoken,
       data   type gs_data2,
       state  type string,
       status type string,
     end of gs_get_accesstoken.


start-of-selection.
*  perform zf_k3c_001. "需要传输的数据 json_data 
  perform get_accesstoken. ""获取密钥
  if gv_accesstoken <> ''.
    perform pr_save_voucher using gv_accesstoken. "调用业务逻辑
  endif.

form get_accesstoken.
*获取密钥接口地址
  url = 'http://192.168.90.110:8022/ierp/api/login.do'.   

*accessToken接口数据

  gv_json_in = '{"user":"18905908070","tenantid":"2303","accountId":"1557314466603859968","usertype":"Mobile"}'.  

*创建http客户端
  call method cl_http_client=>create_by_url
    exporting
      url                = url
    importing
      client             = http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.

*设置调用方法 对应自己的方法
  call method http_client->request->set_header_field
    exporting
      name  = 'login'
      value = 'POST'.

*设置传入字符串
  call method http_client->request->set_cdata
    exporting
      data = gv_json_in.

*发送
  call method http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      http_invalid_timeout       = 4
      others                     = 5.

*接收
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

  if sy-subrc = 0.
    apptoken_result = http_client->response->get_cdata( )."提取返回的JSON数据

*解析JSON 转换为结构数据
    call method zcl_json=>deserialize
      exporting
        json        = apptoken_result
        pretty_name = 'X'
      changing
        data        = gs_get_accesstoken.
    if gs_get_accesstoken-state = 'success'.
      gv_accesstoken = gs_get_accesstoken-data-access_token.  "获取密钥 
    endif.

  else.
    call method http_client->get_last_error   "错误信息
      importing
        code    = l_sysubrc
        message = l_error_text.
  endif.
endform.
form pr_save_voucher  using p_accesstoken.
  clear:url,apptoken_result.

  url = 'http://192.168.90.110:8022/ierp/kapi/v2/twh/gl/gl_voucher/savesap'.


*创建http客户端
  call method cl_http_client=>create_by_url
    exporting
      url                = url
    importing
      client             = http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.

  http_client->request->set_content_type( content_type = 'application/json;charset=utf-8' ).  "其他的一些格式text/html  text/json
*设置http method 为post 
  http_client->request->set_method( if_http_request=>co_request_method_post ).

*  设置token
  call method http_client->request->set_header_field
    exporting
      name  = 'accesstoken'
      value = p_accesstoken.      "传Token

*设置调用方法(根据自己的方法来)
  call method http_client->request->set_header_field
    exporting
      name  = 'savesap'   "调用的接口方法
      value = 'POST'.

*设置传入字符串
  call method http_client->request->set_cdata
    exporting
      data = json_data.

*发送
  call method 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.
    break-point.
  endif.

*接收
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

  if sy-subrc = 0.
    apptoken_result = http_client->response->get_cdata( )."提取返回的JSON数据
    cl_demo_output=>display_json( apptoken_result ).  "用来显示结果 之后可以去掉
  else.
    call method http_client->get_last_error
      importing
        code    = l_sysubrc
        message = l_error_text.
  endif.
endform.                    " PR_SAVE_VOUCHER

 

posted @ 2023-11-16 15:22  肆意点  阅读(98)  评论(0编辑  收藏  举报