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

Send Image CL_HTTP_CLIENT

sid_07_
Explorer
0 Likes
4,902

Hello Experts,

I need to send an image via the post method of cl_http_client. The target system is expecting the image in binary format. I have tried using 'SCMS_XSTRING_TO_BINARY' but this gives the result in hexadecimal format. I then tried passing xstring and base 64 encoded file but the target system is unable to convert it. Is there anyway to achieve this?

This is Service Now API, I am trying to add an attachment to an Incident. This is the link for API documentation.

https://developer.servicenow.com/dev.do#!/reference/api/rome/rest/c_AttachmentAPI#attachment-POST-fi...

I am using POST/now/attachment/file

I need to add the image in request body. here is the documentation screenshot for your reference.

Below is the sample code I am using


REPORT ztest_api_call.

DATA:
  ls_desc        TYPE soli,
  lv_response    TYPE string,
  lv_data        TYPE string,

*   Data Declarations for http client.
  lo_http_client TYPE REF TO if_http_client,
  lo_rest_client TYPE REF TO cl_rest_http_client,

*   Data Declarations for http client header.
  lt_headers     TYPE tihttpnvp,
  ls_headers     LIKE LINE OF lt_headers.

* create an instance of type CL_HTTP_CLIENT to perform the HTTP communication in ABAP
DATA(lv_url) = 'https://instance.servicenow.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot.jpg'.
cl_http_client=>create_by_url(
  EXPORTING
    url = CONV string( lv_url )
  IMPORTING
    client = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active = 2
    internal_error = 3
    OTHERS = 4 ).

CHECK lo_http_client IS BOUND.
* Disable the authentication Pop-up
lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.

* Authenticate using the Service now user id and pwd.
CALL METHOD lo_http_client->authenticate
  EXPORTING
    username = 'username'
    password = 'password'.

* Set method as post method.
lo_http_client->request->set_method(
  EXPORTING
    method = if_http_entity=>co_request_method_post ).

* Set content type as json
lo_http_client->request->set_content_type(
  EXPORTING
    content_type = 'image/png' ).

*Get the current active screen as screen shot.
CALL METHOD cl_gui_frontend_services=>get_screenshot
  IMPORTING
    mime_type_str        = DATA(lv_mime)
    image                = DATA(lv_img)
  EXCEPTIONS
    access_denied        = 1
    cntl_error           = 2
    error_no_gui         = 3
    not_supported_by_gui = 4
    OTHERS               = 5.

lo_http_client->request->set_cdata(
  EXPORTING
    data =  CONV string( lv_img ) ).

*Prepare header of the API call.
CREATE OBJECT lo_rest_client
  EXPORTING
    io_http_client = lo_http_client.

ls_headers-name  = lc_content_type..
ls_headers-value = 'image/png'.
APPEND ls_headers TO lt_headers.

CALL METHOD lo_rest_client->if_rest_client~set_request_headers
  EXPORTING
    it_header_fields = lt_headers.

* Send and recieve the data to and from Service now API
lo_http_client->send(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state = 2 ).


CHECK sy-subrc = 0.
lo_http_client->receive(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3 ).

* Response manipulation to get the incident number.
lv_response = lo_http_client->response->get_cdata( ).


Regards,

Siddhesh

View Entire Topic
Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert

Hello Siddhesh,
you should not use method REQUEST->SET_CDATA. It is for character data. Instead use REQUEST->SET_DATA which is for binary data.

Description from class CL_HTTP_REQUEST:

  • IF_HTTP_ENTITY~SET_CDATA - Sets the HTTP body of this entity to the given char. data
  • IF_HTTP_ENTITY~SET_DATA - Sets the HTTP body of this entity to the given binary data

Otherwise your code seems to be OK, but it is hard to read. You should use "CODE" tool in your post editor to properly format the code.

sid_07_
Explorer

Thanks tomas.buryanek, the solution worked. Going forward I will make use of the CODE tool.

Sandra_Rossi
Active Contributor
0 Likes

sid_07_ You can even edit your question right now and fix it with the CODE tool, to be kind with today and future visitors of your question.

sid_07_
Explorer
0 Likes

Thanks sandra.rossi, I have updated the code and documentation link.