Skip to Content
Technical Articles
Author's profile photo Palanikumar Subramanian

How to send a Odata Batch Request to SAP Cloud for Customer system using Postman Tool

Overview:

In this blog post,we are going to see how to send a Odata Batch Request to the SAP Cloud for Customer system using POSTMAN Tool.

Answers to expect from this post?

  1. How to use batch request in the POSTMAN Tool
  2. What all are the Pre-requisites needs to be done before making a batch call.
  3. What is the format we need to follow in a batch Payload.
  4. What all are the header details needs to be maintained.

Lets get started:

Step 1 :

Login to the Postman Tool using your credentials.

In the Authorization Tab, Enter your credentials as below:

Step 2 :

The Next step is to fetch the CSRF Token.

CSRF Token is necessary in order to update or create the records in the C4C System.

For a GET Call, you don’t need a CSRF Token.

In order to get the CSRF Token,Go to the Header Tab.

Enter ‘x-csrf-token’ in the key and ‘Fetch’ in the Value as below:

After this, you have to make a get call to the C4C System.
Choose the action as GET and enter the URL to make the GET call to C4C System.

URL :

https://myXXXXXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/ServiceRequestCollection(‘YYYYYYYYYYY’)

XXXXXX – Tenant ID
YYYYYYYYYYYYY – Object id of a particular record in the Service Request Collection.

Note: You can use any collection to get the CSRF Token.

Once the GET Call is made, Go to the Headers Tab and Copy the x-csrf-token.

Now, navigate to the Top Header Tab and Paste the CSRF Token as below:

We have completed the Pre-requisite to post a record in the system.

 

Step 3 :

Now, we are going to create 3 Service Requests in the system in a single batch call.

Choose the Action as POST and enter the URL as below:

https://myXXXXXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/$batch

In case of batch call, you don’t need to mention the Collection name as you will mention the collection name in the Payload.

Go the Tab Body and Choose raw.

Copy paste the below Sample Payload.

This Payload will create three Service Requests in the System. We are passing only the Name field to create a record in the system, as it is the only mandatory field to create a Service Request.

Sample Payload for creating three records in the Batch Call :

--batch
Content-Type: multipart/mixed; boundary=changeset

--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary

POST ServiceRequestCollection HTTP/1.1
Content-Type: application/json
Content-ID: 2
Content-Length: 10000

{
"Name":"Testing 1"
}

--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary

POST ServiceRequestCollection HTTP/1.1
Content-Type: application/json
Content-ID: 2
Content-Length: 10000

{
"Name":"Testing 2"
}

--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary

POST ServiceRequestCollection HTTP/1.1
Content-Type: application/json
Content-ID: 2
Content-Length: 10000

{
"Name":"Testing 3"
}

--changeset--
--batch--

It is important to maintain the content type in the Header, for the system to recognize the Payload.

 

Once you add the Payload and the Headers, Click “SEND’ to send the request to the C4C System.

The request is successful, only when you receive a response back as below:

Conclusion:

If you get any error or if you are not getting any response back, check the below points :

  1. The Payload is Space Sensitive, it is mandatory to maintain the proper spacings.
  2. You are opening the Payload with –batch and it should be closed with –batch—
  3. The Changeset needs to be used only if you are changing the data in the database using POST, PATCH, PUT.
  4. Changeset is not required to fetch a data from the database.

 

Assigned tags

      16 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Raphael Lechner
      Raphael Lechner

      Hi Palanikumar,

      thanks for the article.

      For Step 2: Did you check the blog from Andrei Vishnevsky for autofill CSRF-Token? If you do it with the Script you never need consider the csrf token anymore as it's done via the script.

      https://blogs.sap.com/2019/08/27/csrf-token-in-postman.-one-click-to-get-it-and-use-it./

       

      Best regards,

      Raphael

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Raphael,

      Thanks for the suggestion. I will check and try to add a note.

      Regards,

      Palani.

      Author's profile photo sunny kumar
      sunny kumar

      Hi PalaniKumar,

       

      Could you please tell me how to use Patch request in $batch option.If you give an example that would be great.

       

      Regards,

      Sunny

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Sunny,

      You just have to replace POST with PATCH and then pass the desired payload. For Patch, You need to pass the Object ID. In the below example, XXXXXXXXXXXX is the Object ID.

      Example :

       

      --changeset
      Content-Type: application/http
      Content-Transfer-Encoding: binary
      
      PATCH ServiceRequestCollection('XXXXXXXXXXXXXXXXXX') HTTP/1.1
      Content-Type: application/json
      Content-ID: 2
      Content-Length: 10000
      
      {
      "Name":"Testing 2"
      }
      Author's profile photo Mohanbabu KJ
      Mohanbabu KJ

      Hello Palani,

       

      Thank you for your Post. I'm trying to have batch operation for Account which includes services CorporateAccountCollection, ObjectIdentifierMappingCollection and CorporateAccountIdentificationCollection.

       

      Here is my question post: https://answers.sap.com/questions/13114564/batch-operation-in-odata-for-account.html

       

      Can you please check and let me know what I might be doing wrong?

       

      Thanks.

       

      Regards,

      MB

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Mohan,

      CorporateAccountIdentification is a child entity of CorporateAccount. It is not possible to create data for child without using parent reference. 

      In order to achieve this in Batch, you need to send the payload in Deep Insert format instead of sending it as a separate POST.

      Sample Payload, Which worked in my system:

      --batch
      Content-Type: multipart/mixed; boundary=changeset
      
      --changeset
      Content-Type: application/http
      Content-Transfer-Encoding: binary
      
      POST CorporateAccountCollection HTTP/1.1
      Content-Type: application/json
      Content-ID: 2
      Content-Length: 10000
      
      { "Name":"Testing Batch Operation MB", 
        "RoleCode":"XXXXXX", 
        "CountryCode":"US", 
        "CorporateAccountIdentification" :
      
      [{ "IDTypeCode":"XXXXXX", "IDNumber":"9999999999" }]
      }
      
      --changeset--
      --batch--

       

      Regards,

      Palani.

      Author's profile photo Kunal Surati
      Kunal Surati

      Hello Palani, your blog is very useful. I am trying to insert multiple records with batch method from Postman tool. However, I am getting 202 Accepted in response in Post method instead of 201 Created. I followed payload syntax and it’s still the same. Do you know what am I doing wrong?

      My service name is “Fuel”. Below is the service URL.

      https://<hostname:port>/<foldername>/applications/GIRW250/service/girw250bulkfuel.xsodata/Fuel/

      and below is my Payload body:

       

      --batch
      Content-Type: multipart/mixed; boundary=changeset

      --changeset
      Content-Type: application/http
      Content-Transfer-Encoding: binary

      POST Fuel HTTP/1.1
      Content-Type: application/json
      Content-ID: 2
      Content-Length: 10000

      {
      “ID”:” ”
      }

      --changeset--
      --batch--

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Kunal,

      Kindly check whether you have maintained the content type in the Header.

      Regards,

      Palani.

      Author's profile photo Kunal Surati
      Kunal Surati

      Hi Palani, yes I am maintaining following as Content-Type header for Post method

      multipart/mixed;boundary=batch

      But now I am getting different error

      HTTP/1.1 400 Bad Request
      Content-Type: application/xml;charset=utf-8
      Content-Length: 250

      <?xml version="1.0" encoding="utf-8" standalone="yes"?><error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><code/><message xml:lang="en-US">Error processing request stream. JSON text specified is not valid.</message></error>

       

      Regards,

      Kunal

      Author's profile photo Johan de Zwart
      Johan de Zwart

      Hi Kunal,

      Did you find a solution for this?

      regards,

      Johan

      Author's profile photo Johan de Zwart
      Johan de Zwart

      I found the solution. I had the wrong boundry in the header of the request

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Kunal,

      It looks like you are passing a blank value in the field ID. Kindly make a normal POST call and check whether the record is getting created.

      Regards,

      Palani.

      Author's profile photo Kunal Surati
      Kunal Surati

      Hi Palani, Id is autogenerated sequence number from the service. Issue was missing batch format for every new POST changeset. Check below blog:

       

      https://answers.sap.com/questions/13186072/sap-hana-xsa-odata-service-post-method-for-batch-i.html

       

       

       

       

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Kunal,

      If it auto generated, You do not have to pass the ID field in the Request. But, if there is any mandatory field required to create data, you need to pass the same.

      Example :

      In the link, you have shared they are passing the below two fields, Which might be mandatory

      {"CustomerID":"456","Name":"Potter"}
      
      
      Regards,
      Palani.
      
      
      Author's profile photo Surinder g
      Surinder g

      Hi Palani,

        your blog is very fruitful. I'm trying to have batch operation to get the sales order but facing bad request error.

       

       

       

      Can you please check and let me know what I might be doing wrong in the request payload?

       

      Thanks.

       

      Regards,

      G Surinder

       

       

      Author's profile photo Palanikumar Subramanian
      Palanikumar Subramanian
      Blog Post Author

      Hi Surinder,

      It looks like the issue is happening because of the filter (Salesorder= '5000122')

      You need to use the filter similar to the normal GET call.

      SalesOrder?$filter=Salesorder eq '5000122'

      Regards,

      Palani