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.

相关推荐
liliangcsdn8 分钟前
IC计算-前向/远期收益计算和代码示例
开发语言·python·pandas
小溪学编程23 分钟前
Java ListIterator 接口详解:双向遍历与列表修改的利器
java·开发语言·windows
影视飓风TIM23 分钟前
C++11 新特性:Lambda表达式、std::function与std::bind
开发语言·c++
sbjdhjd28 分钟前
在 8 字符、字母数字过滤与 PHP 版本差异下复盘临时文件命令执行链 | (8字符绕过)
android·java·开发语言·安全·web安全·数据挖掘·php
统计学小王子1 小时前
数学建模国赛倒计时 1 天 ——《统计模型精讲(逐步回归 R 语言实战篇)》
开发语言·数学建模·r语言
ttwuai1 小时前
Go 后台接入单点登录后,JWT、菜单权限和接口 403 怎么验?
开发语言·后端·golang
qeen871 小时前
【C++】C++中的错误处理机制-异常
开发语言·c++·异常
步行cgn1 小时前
OCP开闭原则:面向对象设计的核心原则
java·开发语言·开闭原则
曹牧1 小时前
C#:模态对话框
开发语言·c#
en.en..1 小时前
Linux wait()函数(预防僵尸进程)
java·大数据·开发语言