Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

External REST API getting stuck in Receive Method

rvinod1982
Contributor
0 Likes
2,510

Hello,

I am getting data from a external system by calling the API created by the external system.

I have created an ABAP program and using CL_HTTP_CLIENT class for this. The steps being followed are as below:

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_full_url "( Here URL of the API is passed)
IMPORTING
client = o_http_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.
* Implement suitable error handling here
ENDIF.

CALL METHOD o_http_client->request->set_method
EXPORTING
method = 'POST'.

o_http_client->request->set_header_field(
EXPORTING
name = 'Accept'
value = '*/*'
).

o_http_client->request->set_header_field(
EXPORTING
name = 'Content-Type'
value = 'application/json'
).

o_http_client->request->set_header_field(
EXPORTING
name = 'ApiOwner'
value = 'testowner'
).

o_http_client->request->set_header_field(
EXPORTING
name = 'ApiKey'
value = 'testkey'
).

CALL METHOD /ui2/cl_json=>serialize
EXPORTING
data = gs_op_req "( This structure contains the Request Body)
compress = abap_true
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
RECEIVING
r_json = DATA(lv_json). "( The Request is converted to Json Format)

o_http_client->request->set_content_type( gc_app_json ).

CALL METHOD o_http_client->request->set_cdata
EXPORTING
data = lv_json.

CALL METHOD o_http_client->send
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
http_invalid_timeout = 4 " Invalid Time Entry
OTHERS = 5.

CALL METHOD o_http_client->receive "( This method is running indefinitely)
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
OTHERS = 4.

CALL METHOD o_http_client->response->get_status
IMPORTING
code = gv_http_status_code
reason = gv_http_status_text.

DATA: lr_data TYPE REF TO data.

gv_response_content = o_http_client->response->get_cdata( ).

CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = gv_response_content
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
assoc_arrays = abap_true
CHANGING
data = lr_data.

Many times the program gets stuck in the Receive method indefinitely and then the program needs to be terminated.

Then if the program is executed again with the same Request, it gets executed successfully.

The program is scheduled in background with a frequency of every 10 minutes.

What could be the reason of the program getting stuck in the Receive method.

Any help in this regard would be very much appreciated.

Regards,

Vinod Iyer

Hello,

I am getting data from a external system by calling the API created by the external system.

I have created an ABAP program and using CL_HTTP_CLIENT class for this. The steps being followed are as below:

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_full_url "( Here URL of the API is passed)
IMPORTING
client = o_http_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.
* Implement suitable error handling here
ENDIF.

CALL METHOD o_http_client->request->set_method
EXPORTING
method = 'POST'.

o_http_client->request->set_header_field(
EXPORTING
name = 'Accept'
value = '*/*'
).

o_http_client->request->set_header_field(
EXPORTING
name = 'Content-Type'
value = 'application/json'
).

o_http_client->request->set_header_field(
EXPORTING
name = 'ApiOwner'
value = 'testowner'
).

o_http_client->request->set_header_field(
EXPORTING
name = 'ApiKey'
value = 'testkey'
).

CALL METHOD /ui2/cl_json=>serialize
EXPORTING
data = gs_op_req "( This structure contains the Request Body)
compress = abap_true
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
RECEIVING
r_json = DATA(lv_json). "( The Request is converted to Json Format)

o_http_client->request->set_content_type( gc_app_json ).

CALL METHOD o_http_client->request->set_cdata
EXPORTING
data = lv_json.

CALL METHOD o_http_client->send
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
http_invalid_timeout = 4 " Invalid Time Entry
OTHERS = 5.

CALL METHOD o_http_client->receive "( This method is running indefinitely)
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
OTHERS = 4.

CALL METHOD o_http_client->response->get_status
IMPORTING
code = gv_http_status_code
reason = gv_http_status_text.

DATA: lr_data TYPE REF TO data.

gv_response_content = o_http_client->response->get_cdata( ).

CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = gv_response_content
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
assoc_arrays = abap_true
CHANGING
data = lr_data.

Many times the program gets stuck in the Receive method indefinitely and then the program needs to be terminated.

Then if the program is executed again with the same Request, it gets executed successfully.

The program is scheduled in background with a frequency of every 10 minutes.

What could be the reason of the program getting stuck in the Receive method.

Any help in this regard would be very much appreciated.

Regards,

Vinod Iyer

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
2,162

The code formatting didn't work well. Possibly you may retry by using unformatted paste (Shfit+Ctrl+V).

Read only

Sandra_Rossi
Active Contributor
0 Likes
2,162

What happens if you do it with Postman or other such tool?

Read only

rvinod1982
Contributor
0 Likes
2,162

Hi Sandra,

In Postman tool, the response is coming from the API for the same request sent from the program.

In fact, if the program is executed again the response is coming.

Regards,

Vinod Iyer

Read only

anupcvarghese
Newcomer
0 Likes
2,162

Anybody got a solution for this ?

We have a similar issue, receive method hanging randomly. We are calling CPI from ECC using http client.