SAP ABAP 调用RESTful API测试用例

2020-11-30 10:04:51 浏览数 (1)

代码语言:javascript复制
REPORT ZMJ_GETAPI.
 
 
DATA: LEN         TYPE        I, "发送报文长度
      LEN_STRING  TYPE        STRING,
      URL         TYPE        STRING, "接口地址
      HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, "http客户端
      POST_STRING TYPE        STRING,
      RESULT      TYPE        STRING.
DATA: IT_HEADER  TYPE TIHTTPNVP,
      GV_JSON_IN TYPE STRING VALUE '{"IT_INPUT":[{"CUSTOMER":"12345678","PERNR":"00190935"}]}'.
START-OF-SELECTION.
  URL = 'http://10.*.*.*:8001/api_sapnew/ZIF_DATA'.
  "'http://10.X.X.X:8081/api/test/gettest'.
 
  "创建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' ).
 
  "设置http method 为Get
  HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).
  "IF_HTTP_ENTITY~CO_REQUEST_METHOD_GET
 
  "设置待传输内容长度
  LEN = STRLEN( GV_JSON_IN ).
  "设置调用方法
  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = '~request_method'
      VALUE = 'POST'.
  "设置传入字符串
  CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
    EXPORTING
      DATA   = GV_JSON_IN
      OFFSET = 0
      LENGTH = LEN.
  "发送
  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.
  "提取返回字符串
  RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
  "将字符串中的回车符替换,否则abap将会识别为#
  REPLACE ALL OCCURRENCES OF REGEX 'n' IN RESULT WITH SPACE.
  "获取接口返回的数据
*  RESULT = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
 
  WRITE: RESULT.

0 人点赞