SAP FM HTTP_POST to send a message to push notification server

user3759155 picture user3759155 · Jun 20, 2014 · Viewed 10.7k times · Source

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.

Answer

rplantiko picture rplantiko · Jun 20, 2014

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.