Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ABAP cl_http_client REST - PATCH method

Former Member
0 Likes
7,436

Hello community,

hope someone could help me.

I need to communicate with an external API. I use the class cl_http_client for method POST, DELETE, GET. Now I need to change customer data with method PATCH. PUT will be not supported.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_http
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.

CALL METHOD lo_http->request->set_version(
if_http_request=>co_protocol_version_1_0 ).

CALL METHOD lo_http->request->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json; charset=utf-8'.

CALL METHOD lo_http->request->set_header_field
EXPORTING
name = 'authorization'
value = '4711'.

lv_length = strlen( lv_body ).
CALL METHOD lo_http->request->set_cdata
EXPORTING
data = lv_body
offset = 0
length = lv_length.

CALL METHOD lo_http->request->set_method( 'PATCH' ).

lo_http->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ).

lo_http->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).

lv_response = lo_http->response->get_cdata( ).

The main question is, did someone use the method PATCH for the class cl_http_client?

Or is there another solution, way?

4 REPLIES 4
Read only

SimoneMilesi
Active Contributor
3,568

And the question/issue is...?

Read only

Former Member
3,568

Oh sorry, it did not work!

For example the method POST

CALL METHOD lo_http->request->set_method(CO_REQUEST_METHOD_POST).


The main question is, did someone use the method PATCH for the class cl_http_client?

Read only

Former Member
3,568

SOLUTION!!!

cl_http_client=>create_by_destination(
EXPORTING
destination = iv_dest " Logical destination (specified in function call)
IMPORTING
client = lo_http_client " HTTP Client Abstraction
EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5
OTHERS = 6
).

CREATE OBJECT lo_rest_client
EXPORTING
io_http_client = lo_http_client.

lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

CONDENSE lv_url NO-GAPS.


cl_http_utility=>set_request_uri(
EXPORTING
request = lo_http_client->request " HTTP Framework (iHTTP) HTTP Request
uri = lv_url " URI String (in the Form of /path?query-string)
).

* Set Payload or body ( JSON or XML)
lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request->set_header_field( iv_name = `authorization` iv_value = lv_token ).
lo_request->set_string_data( lv_body ).

* Enhancement to Class CL_REST_HTTP_CLIENT - Method PATCH

lo_rest_client->patch( lo_request ).


* Collect response
lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
lv_response = lo_response->get_string_data( )

x

x

x

Coding Method PATCH in Class CL_REST_HTTP_CLIENT

send_receive( iv_http_method = 'PATCH' io_entity = io_entity ).

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
3,568

Nice! 🙂

That CL_REST_HTTP_CLIENT-PATCH is yours "enhancmenet method" ?

By the way, there is IF_REST_MESSAGE-GC_METHOD_PATCH constant, but it is not used like the others:

CL_REST_HTTP_CLIENT

Methods:
IF_REST_RESOURCE~POST
send_receive( iv_http_method = if_rest_message=>gc_method_post io_entity = io_entity ).

IF_REST_RESOURCE~PUT
send_receive( iv_http_method = if_rest_message=>gc_method_put io_entity = io_entity ).

... but not ~PATCH
-- Tomas --