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.

相关推荐
LDR0062 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术2 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园2 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob2 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享2 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.2 天前
C语言--day30
c语言·开发语言
何以解忧,唯有..2 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽2 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下2 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
飞天狗1112 天前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)
java·开发语言