Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SAP ABAP call WEBAPI?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how SAP ABAP calls WEBAPI". In daily operation, I believe many people have doubts about how SAP ABAP calls WEBAPI. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how SAP ABAP calls WEBAPI". Next, please follow the editor to study!

JSON definition

JSON (JavaScript Object Notation) is a lightweight data exchange format. In a nutshell, JSON converts data into strings that are passed between terminals.

Data in name / value pairs, data separated by commas, curly braces save objects, square brackets save arrays

JSON values can be: number (integer or floating point), string (in double quotes), logical value (true or false), array (in square brackets), object (in curly braces), null

For example:

{

"name": "Xiaoqiang"

"age": 16

"msg": ["a", "b"]

"regex": "^ http://.*""

}

Comparison of differences between JSON and XML

1. Definition introduction

(1) .XML definition

Extensible markup language (Extensible Markup Language, XML), which is used to mark electronic documents with structural markup language, can be used to mark data and define data types. It is a source language that allows users to define their own markup language. XML uses DTD (document type definition) document type definitions to organize data; uniform format, cross-platform and language, has long been a recognized standard in the industry.

XML is a subset of the Standard Universal markup language (SGML) and is well suited for Web transport. XML provides a unified way to describe and exchange structured data independent of applications or vendors.

(2) .JSON definition

JSON (JavaScript Object Notation) is a lightweight data interchange format that is readable and easy to write quickly. Data can be exchanged between different platforms. JSON adopts highly compatible and completely independent text format, and also has the behavior similar to C language habits (including C, Cellular, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

JSON is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition-December 1999.

Advantages and disadvantages of 2.XML and JSON

(1) advantages and disadvantages of .XML

Advantages of .XML

a. The format is uniform and meets the standard.

b. It is easy to interact with other systems remotely, and the data sharing is more convenient.

Disadvantages of .XML

The A.XML file is huge, the file format is complex, and the transmission takes up the bandwidth.

b. Both server-side and client-side require a lot of code to parse XML, which makes the server-side and client-side code extremely complex and difficult to maintain.

c. The way of parsing XML between different browsers on the client is inconsistent, so you need to write a lot of code repeatedly.

d. Server-side and client-side parsing takes more resources and time.

(2) advantages and disadvantages of JSON

Advantages of .JSON:

a. The data format is relatively simple, easy to read and write, the format is compressed, and the bandwidth is small.

b. Easy to parse, client JavaScript can simply read JSON data through eval ()

c. Support multiple languages, including ActionScript, C, client, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other server-side languages to facilitate server-side parsing.

d. In the PHP world, PHP-JSON and JSON-PHP have already appeared, but the programs after PHP serialization are called directly, and the objects and arrays on the PHP server can directly generate JSON format, which is convenient for the client to access and extract.

e. Because JSON format can be used directly for server-side code, it greatly simplifies the amount of code development on server-side and client-side, and does not change the task, and is easy to maintain.

Disadvantages of .JSON

a. It is not as popular and widely used as XML format, and it is not as universal as XML.

At present, the promotion of B.JSON format in Web Service is still in its infancy.

Comparison of advantages and disadvantages between 3.XML and JSON

(1)。 Readability.

The data readability of JSON and XML is basically the same, while the readability of JSON and XML is about the same, with the suggested syntax on the one hand and the standard label form on the other, while XML has better readability.

(2)。 Scalability.

XML is inherently extensible, and so is JSON. There is nothing that XML can extend and JSON can't.

(3)。 The difficulty of coding.

XML has a wealth of coding tools, such as Dom4j, JDom, etc., JSON also has tools provided by json.org, but JSON coding is obviously much easier than XML, even without the help of tools can write JSON code, but to write a good XML is not easy.

(4)。 The difficulty of decoding.

The parsing of XML has to consider the parent node of the child node, which makes people dizzy, while the difficulty of JSON parsing is almost zero. XML really has nothing to say about this.

(5)。 In terms of popularity.

XML has been widely used in the industry, and JSON has just begun, but in the specific field of Ajax, the future development must be that XML gives way to JSON. Ajax should become Ajaj (Asynchronous Javascript and JSON) by then.

(6)。 In terms of analytical means.

JSON and XML also have rich parsing tools.

(7)。 Data volume.

Compared with XML, JSON has smaller data size and faster transmission speed.

(8)。 In terms of data interaction.

The interaction between JSON and JavaScript is more convenient, easier to parse and process, and better data exchange.

(9)。 Data description.

JSON is less descriptive than XML in describing the data.

(10)。 Transmission speed.

JSON is much faster than XML.

Comparison of 4.XML and JSON data formats

(1)。 About lightweight and heavyweight

Lightweight and heavyweight are relative, so where is the heavyweight of XML versus JSON? It should be reflected in parsing. XML currently designs two parsing methods: DOM and SAX.

.DOM

DOM regards a data interchange format XML as a DOM object and needs to read the whole XML file into memory. The principle of JSON and XML is the same, but XML has to consider the parent node and child node. At this point, JSON is much less difficult to parse, because JSON is built on two structures: key/value, a collection of key-value pairs, and an ordered collection of values, which can be understood as an array.

.SAX

SAX does not need to read the entire document to process the parsed content, which is a step-by-step parsing method. The program can also stop parsing at any time. In this way, a large document can be displayed step by step, so SAX is suitable for large-scale parsing. At present, JSON cannot do this.

So, the difference between the light / heavy class of JSON and XML is:

JSON only provides the overall parsing solution, and this method can only work well when parsing less data.

XML provides a step-by-step parsing scheme for large-scale data, which is very suitable for processing large amounts of data.

(2)。 On the difficulty of data format coding and parsing

. In terms of coding.

Although XML and JSON have their own coding tools, JSON coding is simpler than XML. You can write JSON code even without the help of tools, but it is a bit difficult to write good XML code; like XML, JSON is text-based, and they both use Unicode encoding, and it is as readable as the data interchange format XML.

Subjectively, JSON is clearer and less redundant. The JSON website provides a strict description of the JSON syntax, but the description is brief. Generally speaking, XML is more suitable for tagging documents, while JSON is more suitable for data exchange processing.

. In terms of analysis.

In the field of ordinary web applications, developers often worry about the parsing of XML. Whether the server generates or processes XML, or the client uses JavaScript to parse XML, it often leads to complex code and extremely low development efficiency.

In fact, for most Web applications, they do not need complex XML to transfer data at all, and the extensibility claimed by XML has little advantage here, and many Ajax applications even return HTML fragments directly to build dynamic Web pages. Compared with returning XML and parsing it, returning HTML fragments greatly reduces the complexity of the system, but lacks some flexibility. The data exchange format JSON provides greater simplicity and flexibility than XML or HTML fragments. In Web Serivice applications, at least for now, XML still has an unshakable position.

(3)。 Case comparison

Both XML and JSON use structured methods to mark data, so let's make a simple comparison.

. Using XML to represent the data of some provinces and cities in China is as follows:

Copy the code

China

Heilongjiang Province

Harbin

Daqing

Guangdong

Guangzhou

Shenzhen

Zhuhai

Taiwan

Taipei

Kaohsiung

Xinjiang

Urumqi

Copy the code

. Using JSON to represent the data of some provinces and cities in China is as follows:

Copy the code

Var country =

{

Name: "China"

Provinces: [

{name: Heilongjiang, citys: {city: ["Harbin", "Daqing"]}}

{name: "Guangdong", citys: {city: ["Guangzhou", "Shenzhen", "Zhuhai"]}}

{name: "Taiwan", citys: {city: ["Taipei", "Kaohsiung"]}}

{name: "Xinjiang", citys: {city: ["Urumqi"]}}

]

}

Copy the code

In terms of the readability of coding, XML has obvious advantages. After all, human language is closer to this explanatory structure. JSON reads more like a data block and is more difficult to read. However, the language that is difficult for us to read is just suitable for machine reading, so the value of "Heilongjiang" can be read through JSON's index country.provinces [0]. Name.

In terms of the difficulty of handwriting coding, XML is more comfortable, easy to read and easy to write. However, the number of characters JSON written is obviously much less. Without blank tabulation and line wrapping, JSON is a dense collection of useful data, while XML contains a lot of repetitive markup characters.

The above content is reproduced on https://www.cnblogs.com/SanMaoSpace/p/3139186.html

-

Actual case:

Application of ABAP, WEBAPI and JSON

Business background:

Call webapi, enter the employee number, and return the employee information.

Code implementation:

1. Write webapi call subroutine.

* &-* & Form frm_call_webapi*&-- -- * * call webapi*----**-- > Iv_json_input enter the json string *-> iv_service_ur service address (http://****) * request- > set_method (if_http_request= > co_request_method_post). "set the content length to be transferred lv_len = STRLEN (iv_json_input)." set the calling method CALL METHOD lo_http_client- > request- > set_header_field EXPORTING name ='~ request_method' value = 'POST'. "set the incoming string CALL METHOD lo_http_client- > request- > set_cdata EXPORTING data = iv_json_input offset = 0 length = lv_len." send data lo_http_client- > send (EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2). "receive returns lo_http_client- > receive (EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3)." extraction returns the string ev_json_output = lo_http_client- > response- > get_cdata (). "replace the carriage return in the string, otherwise abap will be recognized as # REPLACE ALL OCCURRENCES OF REGEX'\ n'IN ev_json_output WITH space.ENDFORM." frm_call_webapi

2. Call subroutines and pass in parameters

REPORT yaiolos_test_05.INCLUDE zbc_webapi_incl.DATA: gv_service_url TYPE string VALUE 'http://10.100.0.232:8081/api_sapnew/ZRFM_HR_OA_GCQD_KH_MAINTAIN', gv_json_in TYPE string VALUE' {"IT_INPUT": [{"CUSTOMER": "12345678", "PERNR": "00190935"}]}', gv_json_out TYPE string.START-OF-SELECTION. "call webapi PERFORM frm_call_webapi USING gv_json_in gv_service_url CHANGING gv_json_out.END-OF-SELECTION. WRITE gv_json_out.

Yaiolos_test_05.txt

Frm_call_webapi.txt

There is a html tag in the code

It is not convenient for copy to use, please see the attachment above.

3. Result confirmation

Note:

Special characters are often recognized as # in abap and require special handling.

1. Explain regular expressions and how special characters correspond to regular expressions, see the link below

Https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215?fr=aladdin

2. When you encounter the # character in debug and do not know its actual content, check the hexadecimal value, such as 0A00, etc.

At this point, the study on "how to call WEBAPI by SAP ABAP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report