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.

相关推荐
king13598745504 分钟前
LiteRT Tensor API:C++ 编写端侧模型图,XNNPACK、GPU 与 NPU 怎么选?
开发语言·c++·人工智能·chatgpt·架构·aigc
geovindu5 分钟前
rust: Template Method Pattern(续)
开发语言·后端·设计模式·rust·行为模式·板方法模式
外收内放7 分钟前
面向对象高级(封装)
开发语言·python
devilnumber20 分钟前
Java 内部迭代 vs 外部迭代:从原理到实战避坑
java·开发语言
学逆向的28 分钟前
C printf原型解析
c语言·开发语言·网络安全
\光辉岁月/30 分钟前
5.math类
java·开发语言
不像程序员的程序媛38 分钟前
mybatisplus insert dto未指定id时会如何
java·开发语言
BeiGuTing_code1 小时前
C字符输出:一个换行符引发的“失踪案”
c语言·开发语言
我命由我123451 小时前
5-2.Python 编码进阶 - 字符串进阶(拼接、普通格式化、普通格式化数字精度控制、快速格式化、遍历、常用方法)
java·开发语言·后端·python·学习·java-ee·学习方法
http阿拉丁神猫5 小时前
Go语言入门笔记:从基础到开发环境搭建(二)
开发语言·笔记·golang