ABAP - call API with x-www-form-urlencoded

怎么在ABAP里调用API,传入x-www-form-urlencoded参数,比如在postman里是这样传的:

TYPES: BEGIN OF ty_resp,

token_type TYPE string,

expires_in TYPE i,

ext_expires_in TYPE i,

access_token TYPE string,

END OF ty_resp.

DATA: lo_client TYPE REF TO if_http_client,

lt_form_fields TYPE tihttpnvp,

lv_status_code TYPE i,

lv_response TYPE string,

ls_resp TYPE ty_resp.

**********************************************************************

CALL METHOD cl_http_client=>create_by_url

EXPORTING

url = gv_token_url

IMPORTING

client = lo_client

EXCEPTIONS

argument_not_found = 1

plugin_not_active = 2

internal_error = 3

pse_not_found = 4

pse_not_distrib = 5

pse_errors = 6

OTHERS = 7.

CHECK lo_client IS BOUND.

lo_client->propertytype_logon_popup = lo_client->co_disabled. "Turn Off logon Popup

lo_client->request->set_method( 'POST' ).

lo_client->request->set_content_type( content_type = if_rest_media_type=>gc_appl_www_form_url_encoded ).

lo_client->response->if_http_entity~set_content_type( content_type = if_rest_media_type=>gc_appl_json ).

APPEND VALUE #( name = 'grant_type' value = 'client_credentials' ) TO lt_form_fields.
APPEND VALUE #( name = 'client_id' value = gv_token_client_id ) TO lt_form_fields.
APPEND VALUE #( name = 'client_secret' value = gv_token_client_secret ) TO lt_form_fields.
APPEND VALUE #( name = 'scope' value = gv_token_scope ) TO lt_form_fields.

lo_client->request->set_form_fields( lt_form_fields ).

CALL METHOD lo_client->send(

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

http_invalid_timeout = 4

OTHERS = 5 ).

"get response

CALL METHOD lo_client->receive(

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

OTHERS = 4 ).

CALL METHOD lo_client->response->get_status

IMPORTING

code = lv_status_code.

lv_response = lo_client->response->get_cdata( ).

CALL METHOD /ui2/cl_json=>deserialize

EXPORTING

json = lv_response

pretty_name = /ui2/cl_json=>pretty_mode-none

assoc_arrays = abap_true

CHANGING

data = ls_resp.

gv_access_token = ls_resp-access_token.

相关推荐
mabing99317 分钟前
Qt QMessageBox、QDialogButtonBox中英文翻译动态切换
开发语言·qt
Sagittarius_A*18 分钟前
【RCELABS】Level 17~18 —— PHP命令执行函数与环境变量注入
开发语言·安全·web安全·靶场·php·rce
冻柠檬飞冰走茶20 分钟前
PTA基础编程题目集 7-19 支票面额(C语言实现)
c语言·开发语言·数据结构·算法
破碎的南瓜32 分钟前
靶场中使用到的php函数以及每种漏洞的防御方式
开发语言·php
谁看我谁 狼家二丫1 小时前
局域网文件共享实战:从“账户被禁用”到成功互传文件
开发语言·php
醉城夜风~1 小时前
手撕 C++ string:从零实现一个简易字符串类
开发语言·c++·算法
秋田君1 小时前
QT_QT布局常用类QSplitter窗口分割类与QDockWidget窗口停靠类
开发语言·数据库·qt
老王生涯1 小时前
rust开发环境配置-Windows & GNU
开发语言·windows·rust
Yeauty2 小时前
自建 HLS 第一问:fMP4 还是 TS?用 Rust 在进程内把两种都跑出来
开发语言·后端·rust
颜x小2 小时前
[C#]泛型类与泛型方法
开发语言·c++·c#