Application Development 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: 

how to call http get api in abap

kyoungmi_oh
Explorer
0 Kudos

Dear All.

As I make new program interfacing with http API, 500 error code is occurring.

Calling API via IE is just like this

It's working well :

https://api.mgage.solutions/servxml/XML_parse_API.php?data=%3CMESSAGE%20VER=%221.2%22%3E%3CUSER%20US...

But in abap program, I got 500 error,

Would you please what's problem in my source ??

source code 1)

CALL METHOD cl_http_client=>create_by_destination
  EXPORTING
    destination           = 'MGAGE_BULK'
  IMPORTING
    client                = lo_client
  EXCEPTIONS
    destination_not_found = 1
    internal_error        = 2.


request = lo_client->request.


CALL METHOD lo_client->request->set_method(
  if_http_request=>co_request_method_post ).


CONCATENATE
'<MESSAGE VER="1.2">'
'<USER USERNAME="*****"' 'PASSWORD="*****" DLR="0"/>'
'<SMS TEXT="This is first sms" ID="1" CUST_MID="customer-reference-uniqueid-20011234567890" MTAG="unique_message_tag_1">'
'<ADDRESS FROM="MGAGE" TO="+918826466" SEQ="1"/>'
'</SMS>'
'<SMS TEXT="This is second sms" ID="2" CUST_MID="customer-reference-uniqueid-20011234567891" MTAG="unique_message_tag_2">'
'<ADDRESS FROM="MGAGE" TO="+91882466" SEQ="1"/>'
'</SMS>'
'</MESSAGE>'
INTO lv_bodystr.


name = 'data'.
value = lv_bodystr.
CALL METHOD request->set_form_field
  EXPORTING
    name  = name
    value = value.


CALL METHOD lo_client->send
  EXCEPTIONS
    http_communication_failure = 1
    OTHERS                     = 4.


IF sy-subrc EQ 0.
  lo_client->receive(
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4 ).


  lo_client->response->get_status( IMPORTING  code = lv_http_code
                                      reason = lv_reason ).


  lv_cdata = lo_client->response->get_cdata( ).

9 REPLIES 9

Sandra_Rossi
Active Contributor
0 Kudos

Could you please format your code with CODE button.

kyoungmi_oh
Explorer

first time to register question here.

modify it now 🙂

TudorRiscutia
Active Participant

Hi Kyoungmi,

As you are attempting to consume an HTTPS service, you will first need to configure the security certificate in tcode STRUST (under SSL-client Anonymous). You can see and download the certificate by clicking on the lock icon next to the URL in Internet Explorer. Simply download and import it in your SAP system. Assuming that everything else is configured correctly, this should solved the issue.

Additionally, check for a more detailed message in tcode SMICM.


BR,

Tudor

0 Kudos

Hi, Tudor

We already set up certificate for this URL.

What I want to know is whether calling API via abap is correct or not.

It not, what's problem on it.

Thanks in advance.

BR.KM

Sandra_Rossi
Active Contributor

Isn't the HTTP error code 500 an "application error" (i.e. which occurs in the called system), so don't you get another message in the variable lv_cdata?

As said by Tudor, use SMICM to trace the request sent + errors (eventually increase the trace level too).

Maybe, you should call the following method, because you use special characters < and > which should be passed with values %3C and %3E (AKA "URL encoding"):

request->SET_FORMFIELD_ENCODING( request->CO_FORMFIELD_ENCODING_ENCODED ).

0 Kudos

Hi, Sandra

As I set Service No = 443 in sm59, 500 error is not occuring, but 400 error in sm59.

It's because there's no any parameter on sm59 connection test, but in abap program as you can see above, i put parameter on it.

Nevertheless I've got 400 error.

so What I want to know is whether calling API via abap is correct or not.

It not, what's problem on it.

Thanks in advance.

BR.KM

0 Kudos

The code seems correct except what I said. I won't repeat. If it's unclear, please tell us what you don't understand.

PS: 400 means (from Wikipedia + https://tools.ietf.org/html/rfc7231#section-6.5.1 ) : "Bad Request. The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing)."

kyoungmi_oh
Explorer
0 Kudos

In sm59 test..here's result.

This page can’t be displayed

  • Make sure the web address saphtmlp://htmlviewer.sap.com is correct.
  • Look for the page with your search engine.
  • Refresh the page in a few minutes.

0 Kudos

So, in fact your question is all about the configuration of SM59 RFC destination. Unfortunately you don't give any information about your config. You may also get some information in SMICM (Internet Communication Manager) HTTP trace, as we already said.