使用 ABAP 代码消费 SAP Cloud for Customer 系统的 OData 服务

Posted JerryWang汪子熙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 ABAP 代码消费 SAP Cloud for Customer 系统的 OData 服务相关的知识,希望对你有一定的参考价值。


下面这个程序演示了如何使用 SAP ABAP 程序,在 SAP C4C 系统里创建 Opportunity 数据:

REPORT ZCREATE_OPP.

define insert_line.
lv_body = lv_body && &1.
lv_body = lv_body && cl_abap_char_utilities=>newline.
end-of-DEFINITION.

DATA:lo_http_client TYPE REF TO if_http_client,
lv_status TYPE i,
lt_fields TYPE tihttpnvp,
lv_sysubrc TYPE sysubrc.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = https://sap.com/sap/c4c/odata/v1/c4codata/$batch
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.

ASSERT sy-subrc = 0.

CALL METHOD lo_http_client->request->set_method( if_http_request=>co_request_method_post ).

lo_http_client->request->set_header_field( name = Content-Type value = multipart/mixed; boundary=batch_1 ).
lo_http_client->request->set_header_field( name = x-csrf-token value = fA_ZUYNfTF_Jdl_kff2G6A== ).
lo_http_client->request->set_header_field( name = Authorization value = Basic 123456 ).


DATA: lv_body TYPE string.

insert_line --batch_1.
insert_line Content-Type: multipart/mixed; boundary=changeset_1.
lv_body = lv_body && cl_abap_char_utilities=>cr_lf.
*
insert_line --changeset_1.
insert_line Content-Type: application/http.
insert_line Content-Transfer-Encoding: binary.
lv_body = lv_body && cl_abap_char_utilities=>cr_lf.

insert_line POST OpportunityCollection HTTP/1.1.
insert_line Content-Length: 5000.
insert_line Accept: application/json.
insert_line Content-Type: application/json.

lv_body = lv_body && cl_abap_char_utilities=>cr_lf.
insert_line .
insert_line "AccountID": "8000018122",.
insert_line "OwnerID": "8000018122",.
insert_line `"Name": "content": "Testing ticket creation via OData Jerry1"`.
insert_line .
insert_line --changeset_1--.
lv_body = lv_body && cl_abap_char_utilities=>cr_lf.
insert_line --batch_1--.

lo_http_client->request->set_cdata( data = lv_body ).
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.

ASSERT sy-subrc = 0.

CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.

IF sy-subrc <> 0.
CALL METHOD lo_http_client->get_last_error
IMPORTING
code = lv_sysubrc
message = DATA(ev_message).
WRITE: / |error occurred during receive data: ev_message | COLOR COL_NEGATIVE.
RETURN.
ENDIF.

DATA(lv_json) = lo_http_client->response->get_cdata( ).

BREAK-POINT.


以上是关于使用 ABAP 代码消费 SAP Cloud for Customer 系统的 OData 服务的主要内容,如果未能解决你的问题,请参考以下文章

SAP 云平台上的 ABAP 编程环境里如何消费第三方服务

如何在SAP Cloud Platform ABAP编程环境里创建一个employee

最详细的 SAP ABAP Web Service 创建和消费步骤讲解

部署在SAP Cloud Platform CloudFoundry环境的应用如何消费SAP Leonardo机器学习API

使用abapGit在ABAP On-Premises系统和SAP云平台ABAP环境之间进行代码传输

使用ABAP代码创建SAP S/4HANA系统的扩展字段