Simple Sample: Writing an entry to SAP Netweaver Gateway using JSON

Skill Level: Beginner

Time Needed: 20 minutes

I recently assisted someone on the forums when they were having trouble creating records in their SAP ECC instance while using Gateway. I thought I would share some very simple code showing what it takes to write an entry into a service via JSON from iOS (Objective C). The reason the title of the document is “Simple Sample” – there are no frills. Whenever I develop a new app, I start small, get the basic functionality working, and proof out the process before getting too creative.

*** If you do not have your own gateway server, you might be able to connect this to the SAP Gateway System which can be found hereCreate an account and utilize this service *** DISCLAIMER: Untested as the signup form does not work 🙁 

The small application sample below uses this document as a starting point. It describes setting up CRUD (Create, Read, Update and Delete) operations for the Bank account list in your back-end, based on the Bank BAPI. For our example, we will be using the Read and Create functionality of the service. Our example will use a open source repository called AFNetworking. The download can be found here, is ARC compatible and is very easy to add and reference in your project. I will skip the basics of creating a iOS project and adding the reference to AFNetworking (but for reference I am using a “Single View” app template in XCode).

Read Operation from your iOS App

Reading from a OData service using JSON can be done in as little as 7 lines of code in objective C (When using the AFNetworking classes). Here is my example code which can be used to read from your service. In my case, I am returning a list of Banks. Below is the code and the output.

Screen Shot 2013-05-10 at 8.20.25 AM.png
Screen Shot 2013-05-10 at 8.23.55 AM.png

Create Operation from your iOS App

Creating a new record adds a small requirement, in that a CSRF (Cross Site Request Forgery) Token needs to be added to the POST request header. If you notice above, I have added this “fetch” header to my GET request, thus returning the key we need along with our data. (2 birds, 1 stone). Here is the create request. Pending it is successful, it will return the record which have been added to your back-end system. Below is the code and output.

Screen Shot 2013-05-10 at 8.19.28 AM.png
Screen Shot 2013-05-10 at 8.25.14 AM.png

Here is a link to the GitHub project to avoid you writing all of this out. Once again – download and follow this useful guide from Gateway team to create the Bank service in your Gateway system, download the project from GitHub, update your gateway server name and details and this will be a useful starting point for creating your next enterprise app.

Disclaimer: Keep in mind the code purposely simplifies the code needed. If you feeling more adventurous:

  1. Ensure your gateway server is using HTTPS.
  2. Instead of using a NSString *dataset, use NSDictionary object/Class to the Request using JSONRepresentation
  3. Add your username and password to the header request, Base64 encoding them.
  4. Instead of writing the JSON response out to a UITextView, add some UITextBoxes and parse the values out.

Can anyone spot the field I forgot to post?