on ‎2023 Feb 27 5:08 PM
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.
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
Help others by sharing your knowledge.
AnswerRequest clarification before answering.
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks tomas.buryanek, the solution worked. Going forward I will make use of the CODE tool.
| User | Count |
|---|---|
| 15 | |
| 9 | |
| 6 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.