Skip to Content
0

HOW TO CONSUME REST SERVICES IN JSON FORMAT

Dec 01, 2016 at 09:57 PM

59

avatar image

HI EXPERTS,

I NEED TO CONSUME REST SERVICE URL AND FETCH THE OUTPUT IN JSON FORMAT,BUT AM GEETING 404 ERROR

HERE IS MY CODE,

DATA : lr_client TYPE REF TO if_http_client,
lv_json_str TYPE string,
w_string TYPE string,
wf_string2 TYPE string,
lv_json TYPE string.
*
DATA lv_http_status TYPE i.
DATA lv_status_text TYPE string.
DATA lv_resp_data TYPE string.
*
data: rlength type i,

txlen type string .

*DATA: lo_http_client TYPE REF TO if_http_client.
*
*START-OF-SELECTION.
PARAMETERS: p_fname TYPE char30 OBLIGATORY,
p_lname TYPE char30 OBLIGATORY.
**
*"concatenate the input parameters value with the url
CONCATENATE 'http://192.168.*.**:8080/sap/bc/zsubmitalv?'
'FNAME=' p_fname '&'
'LNAME=' p_lname INTO w_string.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = w_string
IMPORTING
client = lr_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.


** Set HTTP header properites
*
*SET REQUEST METHOD
CALL METHOD lr_client->request->set_method(
if_http_request=>co_request_method_get ).

* Set SERVER PROTOCOL
lr_client->request->set_version(
if_http_request=>co_protocol_version_1_0 ).

CALL METHOD lr_client->request->set_header_field
EXPORTING
name = 'Accept'
value = 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'.

CALL METHOD lr_client->request->set_header_field
EXPORTING
name = 'Accept-Language'
value = 'es-es,es;q=0.8,en-us;q=0.5,en;q=0.3'.

CALL METHOD lr_client->request->set_header_field
EXPORTING
name = 'Accept-Charset'
value = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'.

CALL METHOD lr_client->request->if_http_entity~set_formfield_encoding
EXPORTING
formfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw.


"Calling the Webservice
CALL METHOD lr_client->send
EXPORTING
timeout = 200
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.

"Receive the Responce from the Web Service
CALL METHOD lr_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.


"Read HTTP RETURN CODE
CALL METHOD lr_client->response->get_status
IMPORTING
code = lv_http_status
reason = lv_status_text.


lv_resp_data = lr_client->response->get_cdata( ).
WRITE: lv_resp_data.

HERE AM TRYING HTML OUTPUT BUT I DON'T NEED THAT,I NEED IN JSON FORMAT.

Thanks,

Om

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Omprakash Rayasingh Dec 02, 2016 at 01:02 PM
0

Any suggestions??????

Share
10 |10000 characters needed characters left characters exceeded