cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Calling PATCH API method returns 400 error?

simone_decato216
Newcomer
0 Likes
2,052

I'm trying to call a SAP ARIBA API using the PATCH method within a S4 HANA ABAP system, but I always get the 400 code with an error message stating :"contentTypeIncorrect".
The SAP ARIBA API documentation gives a very clear example in ABAP, where it is clear that it should work.

******

DATA: lo_http_client TYPE REF TO if_http_client.
DATA: response TYPE string.

"create HTTP client by url
"API endpoint for API sandbox 
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = 'https://sandbox.api.sap.com/ariba/api/approval/v2/sandbox/{approvableType}/{approvableId}'

  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.

IF sy-subrc <> 0.
  "error handling
ENDIF.

"setting request method
lo_http_client->request->set_method('PATCH').

"adding headers
"API Key for API Sandbox
lo_http_client->request->set_header_field( name = 'APIKey' value = 'jZROiOY0vaQrVnAAUGiA6vhGC6SyPPhh' ).
lo_http_client->request->set_header_field( name = 'DataServiceVersion' value = '2.0' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).


CALL METHOD lo_http_client->request->set_cdata
  EXPORTING
    data = '{\n   "state": "approve",\n   "comment": {\n      "text": "string",\n      "visibleToSupplier": true\n   }\n}'.

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.
  "error handling
ENDIF.

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

WRITE: 'response: ', response.

{ "error": { "code": "contentTypeIncorrect", "message": "Content-Type supported is 'application/json'" } }I tried to use Postam to test the service and I don't get the same error


Have you faced the problem or do you have any suggestions?

Thank you
Simone






Accepted Solutions (0)

Answers (2)

Answers (2)

giovanniklein1
Discoverer

Yes, it is necessary to change the abap code call to a lower level:

l_http_client=>create_by_url(
EXPORTING url = lv_url
IMPORTING client = lo_http
).

lo_http->request->set_method( 'PATCH' ).
lo_http->request->set_header_field( name = '~request_method' value = 'PATCH' ).
lo_http->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_http->request->set_header_field( name = 'APIKey' value = gc_api_key_attach ).
lo_http->request->set_header_field( name = 'Authorization' value = |Bearer { lv_token }| ).

DATA: lv_body_xstr TYPE xstring.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_body
mimetype = 'application/json'
IMPORTING
buffer = lv_body_xstr.


" Insert payload directly
CALL METHOD lo_http->request->set_data
EXPORTING
data = lv_body_xstr
offset = 0
length = xstrlen( lv_body_xstr ).

"Send the request
CALL METHOD lo_http->send.
CALL METHOD lo_http->receive.


CALL METHOD lo_http->response->get_status
IMPORTING
code = lv_status.
lv_error_text = lo_http->response->get_cdata( ).

IF lv_status = 200.
ls_log-ariba_status = lv_sap_stat.
ELSE.
ls_log-log_msg = |HTTP { lv_status }: { lv_error_text }|.
ENDIF.

Marisa3
Discoverer
0 Likes

Hi, Simone,

Do you have any solution? I have the same problem.

Thank you so much

Regards,

Marisa

bsonntag1968
Newcomer
0 Likes

Hi,

Do you have any solution now? I have the same problem.

Thank you much

Regards,

Bernhard

Nilesh_Pandey
Newcomer
0 Likes
Hi, Have you got any solution for this ?
giovanniklein1
Discoverer
0 Likes
yes,
giovanniklein1
Discoverer
0 Likes
yes, it is necessary to change the abap code call to a lower level: l_http_client=>create_by_url( EXPORTING url = lv_url IMPORTING client = lo_http ). lo_http->request->set_method( 'PATCH' ). lo_http->request->set_header_field( name = '~request_method' value = 'PATCH' ). lo_http->request->set_header_field( name = 'Content-Type' value = 'application/json' ). lo_http->request->set_header_field( name = 'APIKey' value = gc_api_key_attach ). lo_http->request->set_header_field( name = 'Authorization' value = |Bearer { lv_token }| ). DATA: lv_body_xstr TYPE xstring. CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING text = lv_body mimetype = 'application/json' IMPORTING buffer = lv_body_xstr. " Insert payload directly CALL METHOD lo_http->request->set_data EXPORTING data = lv_body_xstr offset = 0 length = xstrlen( lv_body_xstr ). "Send the request CALL METHOD lo_http->send. CALL METHOD lo_http->receive. CALL METHOD lo_http->response->get_status IMPORTING code = lv_status. lv_error_text = lo_http->response->get_cdata( ). IF lv_status = 200. ls_log-ariba_status = lv_sap_stat. ELSE. ls_log-log_msg = |HTTP { lv_status }: { lv_error_text }|. ENDIF. ENDIF.