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.

相关推荐
FQNmxDG4S1 天前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
前端老石人1 天前
HTML 字符引用完全指南
开发语言·前端·html
matlab_xiaowang1 天前
Redux 入门:JavaScript 可预测状态管理库
开发语言·javascript·其他·ecmascript
虹科网络安全1 天前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
axng pmje1 天前
Java语法进阶
java·开发语言·jvm
老前端的功夫1 天前
【Java从入门到入土】28:Stream API:告别for循环的新时代
java·开发语言·python
qq_435287921 天前
第9章 夸父逐日与后羿射日:死循环与进程终止?十个太阳同时值班的并行冲突
java·开发语言·git·死循环·进程终止·并行冲突·夸父逐日
止语Lab1 天前
从手动到框架:Go DI 演进的三个拐点
开发语言·后端·golang
yaoxin5211231 天前
397. Java 文件操作基础 - 创建常规文件与临时文件
java·开发语言·python
小短腿的代码世界1 天前
Qt日志系统深度解析:从qDebug到企业级日志框架
开发语言·qt