2

I have a question about the SAP Function Module "http_post". I just want to post a short message (msg) out of the SAP to a Push Notification Server (pushd-Github-Projekt) I installed before. Now I'm not sure how to pass the message.

I tested the FM with the test-symbol:

CALL FUNCTION 'HTTP_POST'
  exporting
    ABSOLUTE_URI                =     uri " Uniform Resource Identifier (RFC 1945)
*    REQUEST_ENTITY_BODY_LENGTH  =     14 "request_entity_body_length
*    RFC_DESTINATION             =     " RFC Destination
*    PROXY                       =     " HTTP Proxy Rechner
*    PROXY_USER                  =     " Benutzername auf dem Proxy Rechner
*    PROXY_PASSWORD              =     " Passwort auf dem Proxy Rechner
*    USER                        =     " Benutzername auf dem HTTP Server
*    PASSWORD                    =     " Passwort auf dem HTTP Server
*    BLANKSTOCRLF                =     " Blanks in CRLF konvertieren im Entity Body
*  importing
*    STATUS_CODE                 =     " Statuscode ( 2xx = OK )
*    STATUS_TEXT                 =     " Text zum Statuscode
*    RESPONSE_ENTITY_BODY_LENGTH =     " Länge vom Response-Entity-Body
  tables
    REQUEST_ENTITY_BODY         =    '{"msg":"test"}' "request_entity_body 
    RESPONSE_ENTITY_BODY        =    '' " Response-Entity-Body Daten
    RESPONSE_HEADERS            =    '' " Header Zeilen vom Response
    REQUEST_HEADERS             =    'Content-Type: application/json' "request_headers
*  exceptions
*    CONNECT_FAILED              = 1
*    TIMEOUT                     = 2
*    INTERNAL_ERROR              = 3
*    TCPIP_ERROR                 = 4
*    SYSTEM_FAILURE              = 5
*    COMMUNICATION_FAILURE       = 6
*    OTHERS                      = 7
  .

I know my values are no tables, but I tested it with the test-symbol, where you can write the values directly in a table. When I start the FM, I get an Bad Request error in the SAP and this error at the push notification server:

SyntaxError: Unexpected token
at Object.parse (native)
at IncomingMessage.<anonymous> ...Path from the pushd..
express\node_modules\connect\lib\middleware\json.js:76:27
at incomingMessage.EventEmitter.emit events.js:92:17
at _stream:readable.js:919:16
at process._tickCallback <node.js:419:13>

Can anyone help me how to pass the request to the FM HTTP-Post? It has to be sth. with msg, because otherwise the Push-Notification-Server can't handle it.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
user3759155
  • 21
  • 1
  • 1
  • 4
  • On which release are you? In my release (SAP_BASIS 731), the interface of the function module looks different, and the test report `RSHTTP30` which was designed for testing `HTTP_POST` has been deleted. – rplantiko Jun 20 '14 at 08:58
  • If available on your system, better send `POST` requests with class `CL_HTTP_CLIENT`. – rplantiko Jun 20 '14 at 09:01
  • ? I meant SAP release (given in menu System->Status, details of component versions, the release and SP level for component `SAP_BASIS`) – rplantiko Jun 20 '14 at 09:26
  • Sorry... I'm a beginner... The Release is 731 und SP Level 0009... I just edited the fm http-post in my question, because I told you something wrong before. Now you should have the same FM... – user3759155 Jun 20 '14 at 09:41

1 Answers1

3

In SAP_BASIS release 731 or higher, I would strongly recommend to use the class CL_HTTP_CLIENTfor doing HTTP requests. See here an example report on how to do it. Replace the dummy string http:1.2.3.4:80/testjon/by your URL in question.

report z_test_http_post.

start-of-selection.
  perform start.

* ---
form start.

  data: lv_status type i,
        lv_error_occurred type flag,
        lv_error_msg type string,
        lv_response_body type string.

  perform send_json using
    'http://1.2.3.4:80/testjson/'  " Use your URL here
    '{"hello":"world"}'            " Use your JSON here
    changing lv_status lv_response_body
             lv_error_occurred
             lv_error_msg.

* Show result
  format color col_heading.
  write: / 'Response status:', lv_status.
  if lv_error_occurred = 'X'.
    format color col_negative.
    write: / 'Error occurred:', lv_error_msg.
  endif.
  format color col_normal.
  write: / 'Response:', lv_response_body.

endform.                    "start

form send_json using iv_url type string
                     iv_json_data type string
        changing cv_status type i
                 cv_response_body type string
                 cv_error_occurred type flag
                 cv_error_msg type string.


  data: lo_client type ref to if_http_client.

  clear: cv_error_msg,
         cv_status,
         cv_error_occurred,
         cv_error_msg.

  if iv_url is initial.
* No URL passed
    message e349(sbds) into cv_error_msg.
    cv_error_occurred = 'X'.
    return.
  endif.

  call method cl_http_client=>create_by_url
    exporting
      url                = iv_url
    importing
      client             = lo_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.
  if sy-subrc ne 0.
    message id sy-msgid type sy-msgty number sy-msgno
      with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
     into cv_error_msg.
    cv_error_occurred = 'X'.
    return.
  endif.

  lo_client->request->set_cdata( iv_json_data ).
  lo_client->request->set_content_type( 'application/json' ).
  lo_client->request->set_method( 'POST' ).
  call method lo_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      others                     = 4.
  if sy-subrc ne 0.
    lo_client->get_last_error( importing message = cv_error_msg ).
    cv_error_occurred = 'X'.
    return.
  endif.

  lo_client->receive( exceptions others = 1 ).
  if sy-subrc ne 0.
    lo_client->get_last_error( importing message = cv_error_msg ).
    cv_error_occurred = 'X'.
    return.
  endif.

  cv_response_body = lo_client->response->get_cdata( ).
  lo_client->response->get_status( importing code = cv_status ).

endform.
rplantiko
  • 2,698
  • 1
  • 22
  • 21
  • Thank, I tried it. I get an error: "ICM_HTTP_CONNECTION_FAILED" Response status: 0 – user3759155 Jun 20 '14 at 10:41
  • Try with a test URL, e.g. `http://google.com` - if it doesn'w work either, you have a connectivity problem with the SAP HTTP port. If it *does* work, the problem is _not_ in the SAP system but in the connection to the other URL (firewalls?) or the server on the other side (what does the access log of the other endoint say?). – rplantiko Jun 20 '14 at 11:39
  • I solved the Problem by taking the FM HTTP_POST. The problem was, that i missed the `RFC_DESTINATION`. It has to be `"SAPHTTP"` – user3759155 Jun 20 '14 at 13:15