In previous episode of my blog I had explained about how we call ‘GET’ method of SAP S/4HANA Cloud API:
In this episode I would explain how to use ‘POST’ method of SAP S/4HANA Cloud API from SAP API Business Hub.
For complete utilization of information present in this blog, it is recommended to go through my earlier blogs as part of this series if not done already.
In our example here, we will create a Purchase Order in SAP S/4HANA Cloud using “Purchase Order” API.
For this we would need following:
1. Connection to our SAP S/4HANA Cloud tenant :
To get information on how we connect SAP API Business Hub to our own SAP S/4HANA Cloud tenant, kindly refer to my blog: https://blogs.sap.com/2017/11/20/getting-started-with-sap-api-business-hub/
2. Data in JSON format to create a Purchase Order:
To prepare this data we can copy the API specific model schema present under ‘POST’ tab (right next to ‘A_PuchaseOrderType’ in our example) and provide valid values against the fields:
Note: The specific details about optional and mandatory fields for each entity in an API can be found in the business documentation linked in the overview tab of SAP API Business hub:
3. CSRF Token:
Let’s see how to get the CSRF token in below mentioned steps:
3.1 Go to ‘GET’ method of API entity(more details of this step can be found in my earlier blog here)
3.2 Click ‘Add’ and then Click ‘Header’ button to add header parameter:
3.3 Fill in the below details in Header:
3.4 Now click ‘Try it out’ button. You will get the x-csrf-token number as one of the entries in Response Headers:
Note: Copy the x-csrf-token value i.e. value mentioned between “[ and ]”. We would need to mention it for POST request.
We will now create a Purchase Order with the help of POST request. Let’s see this in 4 easy steps:
Step1: Click ‘A_PurchaserOrder’ and then click ‘POST /A_PurchaseOrder’:
Step 2: Add a header parameter ‘x-csrf-token’ as shown in point 3.3 and paste the value which we had copied in point 3.4:
Step 3: Put the JSON data prepared in point 2 under the Parameter ‘A_PurchaseOrderType’:
Note: For our example here we will not pass the PO number in input data under “PurchaseOrder” because we would be getting it generated from the system itself under Response Header (might vary based on your number range settings).
Step 4: Click ‘Try it out’ button and a system generated purchase order number can be seen under Response Body for the newly created PO:
The Response code would be displayed as ‘201’ when PO creation is successful.
We can copy this new PO number from Response Body and check the details of it either by calling ‘GET’ method again or by directly logging into your SAP S/4HANA Cloud tenant.
Note: In case we are providing some wrong data, we would be able to see error message in Response Body itself.
We have now successfully created a Purchase Order in SAP S/4HANA Cloud using POST method of “Process Purchase Order” API triggered from SAP API Business Hub.
Disclaimer: This blog is based on SAP S/4HANA Cloud 1711 release.