SAP FM HTTP_POST向推送通知服务器发送消息

时间:2014-06-20 07:36:31

标签: json http-post abap function-module

我对SAP功能模块“http_post”有疑问。 我只是想将一条短信(msg)发布到我之前安装的Push Notification Server(pushd-Github-Projekt)中。现在我不知道如何传递信息。

我用测试符号测试了FM:

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
  .

我知道我的值不是表格,但是我用测试符号对其进行了测试,您可以在表格中直接写入值。 当我启动FM时,我在SAP中错误请求错误 和推送通知服务器上的此错误:

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>

任何人都可以帮我解决如何将请求传递给FM HTTP-Post的问题吗?它必须是......使用msg,因为否则Push-Notification-Server无法处理它。

1 个答案:

答案 0 :(得分:1)

SAP_BASIS版本731或更高版本中,我强烈建议您使用类CL_HTTP_CLIENT来执行HTTP请求。请参阅此处有关如何执行此操作的示例报告。用您的网址替换虚拟字符串http:1.2.3.4:80/testjon/

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.