0% found this document useful (0 votes)
6 views3 pages

Api Call Demo

This document details the process of sending an HTTP request to an API and handling the response. It creates an HTTP client, sets headers, sends JSON data, receives the response, parses the response, and handles success or error cases by logging details to a database table.

Uploaded by

bablu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Api Call Demo

This document details the process of sending an HTTP request to an API and handling the response. It creates an HTTP client, sets headers, sends JSON data, receives the response, parses the response, and handles success or error cases by logging details to a database table.

Uploaded by

bablu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

CLEAR : url, token.

url = wa_url-url.
token = wa_url-token.
lv_custid = |{ wa_url-tdl_id ALPHA = OUT }|.
TRY.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = url
IMPORTING
client = o_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
pse_not_found = 4
pse_not_distrib = 5
pse_errors = 6
OTHERS = 7.
IF sy-subrc <> 0.
o_client->close( ).
ENDIF.
IF o_client IS BOUND.
"Set authprization
SELECT SINGLE * FROM zhcit_url_id INTO wa_url_id WHERE url_name EQ
'IRN' AND gltgv LE sy-datum AND gltgb GE sy-datum.
IF sy-subrc = 0.
id = wa_url_id-id.
passcode = wa_url_id-passcode.
CALL METHOD o_client->authenticate
EXPORTING
username = id
password = passcode.
ELSE.
MESSAGE : 'Please maintain HCI authorization' TYPE 'E'.
ENDIF.
"HEADER FIELDS
o_client->request->set_header_field(
EXPORTING
name = 'token'
value = token ).
o_client->request->set_header_field(
EXPORTING
name = 'customerid'
value = lv_custid ).
o_client->request->set_header_field(
EXPORTING
name = 'Content-Type'
value = 'application/json' ). "lv_custid
CALL METHOD /ui2/cl_json=>serialize
EXPORTING
data = it_body
* PRETTY_NAME = 'C'
RECEIVING
r_json = gs_json.
PERFORM format_json.
lo_http_req = o_client->request.
lo_http_req->append_cdata(
EXPORTING
data = gs_json ).
"Set Timeout
CALL METHOD o_client->send
* EXPORTING
* timeout = CO_TIMEOUT_DEFAULT
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
"Read Response
CALL METHOD o_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
IF sy-subrc = 0.
o_client->response->get_status(
IMPORTING
code = lv_http_status " HTTP Status Code
reason = lv_status_text " HTTP status description
).
IF lv_http_status = 200.
lv_result = o_client->response->get_cdata( ).
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_result
* jsonx =
* pretty_name = PRETTY_MODE-NONE
* assoc_arrays = C_BOOL-FALSE
* assoc_arrays_opt = C_BOOL-FALSE
* name_mappings =
* conversion_exits = C_BOOL-FALSE
CHANGING
data = it_response.
READ TABLE it_response INTO wa_response INDEX 1 .
IF sy-subrc = 0.
wa_log_einv-company_code = ls_qrctab-company_code.
wa_log_einv-customer_number = ls_qrctab-customer_number.
wa_log_einv-document_number = gw_final-docno.
wa_log_einv-fiscal_year = ls_qrctab-fiscal_year .
wa_log_einv-ersda = ls_qrctab-ersda.
wa_log_einv-created_at_time = ls_qrctab-created_at_time .
wa_log_einv-ernam = ls_qrctab-ernam .
IF wa_response-irp_response-data-nic_response_data-irn IS NOT
INITIAL .
wa_log_einv-inv_reg_num = wa_response-irp_response-data-
nic_response_data-irn.
wa_log_einv-qrcode = wa_response-irp_response-data-
nic_response_data-signedqrcode.
wa_log_einv-status = 'SUCCESS'.
ELSE.
it_irn_schema_validation = wa_response-pwc_response-
validation_remarks-irn_schema_validation.
it_business_validations = wa_response-pwc_response-
validation_remarks-business_validations.
it_error_details = wa_response-irp_response-data-error_details.
IF wa_response-irp_response-error = 'X'.
LOOP AT it_error_details INTO wa_error_details .
CONCATENATE wa_log_einv-errdesc wa_error_details-
errormessage ' |' INTO wa_log_einv-errdesc .
ENDLOOP.
ENDIF.
IF it_irn_schema_validation IS NOT INITIAL.
LOOP AT it_irn_schema_validation INTO
wa_irn_schema_validation WHERE validation_status EQ 'Error'.
CONCATENATE wa_log_einv-errdesc wa_irn_schema_validation-
remark ' |' INTO wa_log_einv-errdesc.
ENDLOOP.
ENDIF.
IF it_business_validations IS NOT INITIAL .
LOOP AT it_business_validations INTO wa_business_validations
WHERE validation_status EQ 'Error'.
CONCATENATE wa_log_einv-errdesc wa_business_validations-
remark ' |' INTO wa_log_einv-errdesc .
ENDLOOP.
ENDIF.
wa_log_einv-status = 'ERROR'.
* wa_log_einv-errdesc = wa_response-pwc_response-message.
ENDIF.
wa_log_einv-server = 'PWC'.
ENDIF.
ELSE.
wa_log_einv-company_code = ls_qrctab-company_code.
wa_log_einv-customer_number = ls_qrctab-customer_number.
wa_log_einv-document_number = gw_final-docno.
wa_log_einv-fiscal_year = ls_qrctab-fiscal_year .
wa_log_einv-ersda = ls_qrctab-ersda.
wa_log_einv-created_at_time = ls_qrctab-created_at_time .
wa_log_einv-ernam = ls_qrctab-ernam .
wa_log_einv-status = 'ERROR'.
lv_msg = lv_http_status.
CONCATENATE lv_msg lv_status_text INTO wa_log_einv-errdesc
SEPARATED BY space.
wa_log_einv-server = 'PWC'.
ENDIF.
MODIFY zlog_einv_qrcode FROM wa_log_einv.
ENDIF.
"Close HTTP Connection
o_client->close( ).
ENDIF.
CATCH cx_root INTO DATA(e_txt).
ENDTRY.
ENDIF.
ELSE.
MESSAGE : 'Please maintain URL details.' TYPE 'E'.
ENDIF.
CLEAR :
wa_body,wa_itemlist,wa_precdocdtls ,wa_log_einv,lv_msg,it_response,it_body,it_addld
ocdtls,it_contrdtls,it_precdocdtls,it_itemlist,it_attribdtls,wa_url,gw_final.

You might also like