SAP调用RestfulApi接口接收数据

因为准备要做一个关于调用外部接口的需求,所以自己先练习一下。

程序说明:我已经在.net开发的系统里提供一个api接口,现在在sap访问这个接口,来接收数据。

这里涉及Restful Api知识,以后再分享。

这是一个api地址:http://10.X.X.X:8081/api/test/gettest

首先根据这个url创建一个http客户端

call method cl_http_client=>create_by_url
    exporting
      url                = url
    importing
      client             = http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.

选择一个HTTP GET METHOD

http_client->request->set_method( if_http_request=>co_request_method_get ).

发送和接收数据

 "发送
  call method http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      http_invalid_timeout       = 4
      others                     = 5.

  "接收
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.   

最后就可以获取接收的数据了

  "获取接口返回的数据
  result = http_client->response->get_cdata( ).

  write: result.

输出结果是这样的

跟外部系统返回的数据是一致的。

附上完整代码

data: len         type        i,"发送报文长度
      len_string  type        string,
      url         type        string, "接口地址
      http_client type ref to if_http_client,"http客户端
      post_string type        string,
      result      type        string.

data: it_header type tihttpnvp.



start-of-selection.

  url = 'http://10.X.X.X:8081/api/test/gettest'.


  "创建http客户端
  call method cl_http_client=>create_by_url
    exporting
      url                = url
    importing
      client             = http_client
    exceptions
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      others             = 4.

  "设置http method 为Get
  http_client->request->set_method( if_http_request=>co_request_method_get ).

  "发送
  call method http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      http_invalid_timeout       = 4
      others                     = 5.

  "接收
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

  "获取接口返回的数据
  result = http_client->response->get_cdata( ).

  write: result.

  

 

以后会继续分享用POST方法发送数据到外部接口的例子。  

作者:明光烁亮
出处:http://www.cnblogs.com/hezhongxun/
微信号:HEme922 欢迎加好友一起交流SAP!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/hezhongxun/p/10736964.html

### SAP Web API 接口据接收实现方式 #### 处理SAP Web API接口据或请求 当作为接收方处理来自SAP系统的Web API接口时,通常会涉及通过HTTP协议接收到的SOAP消息或者RESTful风格的服务请求。以下是具体实现的方式: 对于基于SOAP的消息传递机制,可以利用标准的XML解析库来解码并提所需的信息[^1]。例如,在Java环境中可使用JAX-WS工具集;而在.NET框架下,则能够借助WCF (Windows Communication Foundation) 来完成这一过程。 如果采用的是RESTful架构设计模式下的API调用形式——即所谓的ODATA服务—那么开发者应该熟悉如何操作URL参以及JSON格式的据交换方法[^2]。这类场景下推荐运用流行的开源项目如Spring Boot配合Jackson序列化器来进行快速开发部署工作流应用。 下面给出一段Python代码示例用于演示简单的HTTP POST请求响应处理器逻辑: ```python from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/sap/api/receive', methods=['POST']) def receive_sap_data(): content_type = request.headers.get('Content-Type') if 'application/json' in content_type: json_payload = request.json # Process JSON payload here... return jsonify({"status": "success", "message": f"Received {len(json_payload)} records"}), 200 elif 'text/xml' in content_type or 'application/soap+xml' in content_type: xml_payload = request.data.decode() # Parse XML and process accordingly... return "<Response><Status>Success</Status></Response>", 200, {'Content-Type': 'text/xml'} else: return {"error": "Unsupported Media Type"}, 415 if __name__ == '__main__': app.run(debug=True) ``` 此脚本定义了一个Flask web服务器端点`/sap/api/receive`, 它可以根据传入的内容类型自动区分是JSON还是XML(SOAP),从而采不同的策略去解读和回应客户端发送过来的据包[^3]. #### 总结 无论是哪种类型的SAP Web API接口,关键是理解其背后的技术原理,并选用合适的编程语言和技术栈加以支持。上述例子仅提供了一种可能的方向供参考学习之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值