Skip to Content
0
Jul 14, 2020 at 07:28 PM

How to update data in Custom Business Object by OData service method PATCH?

210 Views Last edit Jul 14, 2020 at 07:30 PM 2 rev

Hi, friends.
I've created CBO "ZZ1_MY_CBO" and generated the OData service for this. By Postman I can update any row in this CBO, but when I try to update the row from my ABAP report I always get the same error "The specified HTTP method is not allowed for the resource identified by the Data Service Request URI"!
In postman my URL looks like this:

http://<myservice>:<myport>/sap/opu/odata/sap/ZZ1_MY_CBO_CDS/ZZ1_MY_CBO(guid'0050568d-81c7-1eea-b084-893ea05d70c3')

My ABAP-report code looks like this:

DATA(lv_url) = |http://<myservice>:<myport>/sap/opu/odata/sap/ZZ1_MY_CBO_CDS/ZZ1_MY_CBO(guid'0050568d-81c7-1eea-b084-893ea05d70c3')|.

CALL METHOD cl_http_client=>create_by_url EXPORTING url = lv_url IMPORTING client = lo_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4. lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled. lo_http_client->request->set_method( |'{ iv_odata_method }'| ). LOOP AT it_cookies ASSIGNING FIELD-SYMBOL(<fs_cookie>). lo_http_client->request->set_cookie( name = <fs_cookie>-name path = <fs_cookie>-path value = <fs_cookie>-value domain = <fs_cookie>-xdomain expires = <fs_cookie>-expires secure = <fs_cookie>-secure ). ENDLOOP. lo_http_client->request->set_header_field( name = 'sap-user' value = 'myLogin' ). lo_http_client->request->set_header_field( name = 'sap-password' value = 'MyPassword' <br> lo_http_client->request->set_header_field( name = 'x-csrf-token' value = 'getToken' ). lo_http_client->request->set_header_field( name = 'X-Requested-With' value = 'X' ). lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ). lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ). request = '{"STATUS": "' && |{ iv_status }| && '",' && '"STATUS_REASON": "' && |{ iv_reason }| && '"' && '}'.<br><br> lo_http_client->request->set_cdata( data = request ). CALL METHOD lo_http_client->send EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 http_invalid_timeout = 4 OTHERS = 5. IF sy-subrc = 0. CALL METHOD lo_http_client->receive EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 OTHERS = 5. ENDIF. IF sy-subrc = 0. DATA(lv_response) = lo_http_client->response->get_cdata( ). FIND ALL OCCURRENCES OF REGEX 'error' IN lv_response IGNORING CASE RESULTS DATA(lv_send_error). IF lv_send_error IS NOT INITIAL. rv_result = abap_false. ELSE. rv_result = abap_true. ENDIF. ENDIF.