1

我对 SAP 功能模块“http_post”有疑问。我只想将一条短消息(msg)从 SAP 发布到我之前安装的推送通知服务器(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?它必须是某事。与味精,否则推送通知服务器无法处理它。

4

1 回答 1

3

SAP_BASIS731 或更高版本中,我强烈建议使用该类CL_HTTP_CLIENT来执行 HTTP 请求。请参阅此处有关如何执行此操作的示例报告。用您的 URL替换虚拟字符串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.
于 2014-06-20T10:19:44.350 回答