SAP调用外部URL的时候,部分URL的传入参数为Form-data,带Key,传入数据格式为JSON。
(某xx系统别tm总看我文章发给我们看)213不213啊你们。
此时通过ABAP程序如何传入参数?
ABAP实现过程:
1、建立Http连接。
代码语言:javascript复制URL = '192.168.0.0/test'.
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = URL
* PROXY_SERVICE = '443'
IMPORTING
CLIENT = HTTP_CLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
2、设置请求的Header参数
代码语言:javascript复制*设置内容格式为Form-data
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'Content-Type'
VALUE = 'multipart/form-data'.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~request_method'
VALUE = 'POST'.
3、添加和绑定Form-data参数名
4、将内表数据序列化为JSON格式对象
5、传值
代码语言:javascript复制*定义Form-data value
DATA: IT_FORMULARIO TYPE TIHTTPNVP,
WA_FORMULARIO LIKE LINE OF IT_FORMULARIO,
PART TYPE REF TO IF_HTTP_ENTITY.
PART = HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~ADD_MULTIPART( ).
*传入Value
CALL METHOD PART->APPEND_CDATA
EXPORTING
DATA = V_RESPONSE.
*设置内容格式和name
CALL METHOD PART->SET_HEADER_FIELD
EXPORTING
NAME = 'content-disposition'
VALUE = 'form-data;name="budgetList"'.
完整代码如下参考:
代码语言:javascript复制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.
***系统传参
TYPES: BEGIN OF TY_MATERIAL, "Para
MATNR TYPE STRING,
WERKS TYPE STRING,
END OF TY_MATERIAL.
DATA LS_MATERIAL TYPE TY_MATERIALT.
DATA LT_MATERIAL LIKE TABLE OF LS_MATERIAL.
TYPES: BEGIN OF TY_CODEMSG, "Para
CODE TYPE STRING,
MSG TYPE STRING,
END OF TY_CODEMSG.
DATA LS_CODEMSG TYPE TY_CODEMSG.
DATA LT_CODEMSG LIKE TABLE OF LS_CODEMSG.
START-OF-SELECTION.
" Form-data 参数传值定义
LS_MATERIAL-MATNR= 'TEST001'.
LS_MATERIAL-WERKS = '1000'.
APPEND LS_MATERIAL TO LT_MATERIAL.
URL = '192.168.0.0/TEST'.
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = URL
* PROXY_SERVICE = '443'
IMPORTING
CLIENT = HTTP_CLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
*设置内容格式为Form-data
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'Content-Type'
VALUE = 'multipart/form-data'.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = '~request_method'
VALUE = 'POST'.
*将内表序列化为JSON格式
CLEAR V_RESPONSE .
V_RESPONSE = ZUI2_JSON=>SERIALIZE( DATA = LT_MATERIAL
COMPRESS = ABAP_FALSE
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-LOW_CASE
ASSOC_ARRAYS = ABAP_TRUE
) .
*定义Form-data value
DATA: IT_FORMULARIO TYPE TIHTTPNVP,
WA_FORMULARIO LIKE LINE OF IT_FORMULARIO,
PART TYPE REF TO IF_HTTP_ENTITY.
PART = HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~ADD_MULTIPART( ).
*传入Value
CALL METHOD PART->APPEND_CDATA
EXPORTING
DATA = V_RESPONSE.
*设置内容格式和name
CALL METHOD PART->SET_HEADER_FIELD
EXPORTING
NAME = 'content-disposition'
VALUE = 'form-data;name="budgetList"'.
"发送
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( ).
*反序列化传值到内表或结构
ZUI2_JSON=>DESERIALIZE( EXPORTING JSON = RESULT
ASSOC_ARRAYS = ABAP_TRUE
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-LOW_CASE
CHANGING DATA = LS_CODEMSG
)
.
*
WRITE: 'code:', LS_CODEMSG-CODE , 'message:' ,LS_CODEMSG-MSG.