Quantcast
Channel: SCN : Discussion List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 6412

XSJS Batch Post for OData

$
0
0

Hello,

 

I'm currently working with B1 Version for HANA and try to do a post to the OData Service Layer of Business One from XSJS of Hana. So far it works for regular, single entity Post e.g.:

 

/**

* Logon to B1SL

*/

var b1slDestination = $.net.http.readDestination("test.Connector.imex", "b1sl");

var client = new $.net.http.Client();

var request = new $.net.http.Request($.net.http.POST, "/Login");

request.setBody('{"CompanyDB": "SBODEMODE", "UserName": "manager", "Password": "1234"}');

 

var response = client.request(request, b1slDestination).getResponse();

 

var logonObject = JSON.parse(response.body.asString());

var sessionId = logonObject.SessionId;

 

So, above I issue a POST to the Login of the Business One Service Layer of a demo DB from 9.1; I can then do queries against it just fine, like doing a single insert.

 

When I however want to issue an Batch-Post I suddenly hit a wall. Code is similar to this (note: I stripped down this to just 1 operation till it works), e.g.:

 

/**

* Batch Test

*/

var batchId = "batch_36522ad7";

var changesetId = "changeset_1a5324";

var batchRequest = new $.net.http.Request($.net.http.POST, "/$batch");

batchRequest.contentType = "multipart/mixed; boundary="+ batchId  +" ";

batchRequest.cookies.set('B1SESSION', sessionId);

 

 

 

 

var b2 = "--batch_36522ad7 \r\n" +

    "Content-Type: multipart/mixed; boundary=changeset_1a53241  \r\n" +

    " \r\n" +

    "--changeset_1a53241  \r\n" +

    "Content-Type: application/http  \r\n" +

    "Content-Transfer-Encoding: binary  \r\n" +

    "POST /b1s/v1/Banks HTTP/1.1  \r\n" +

    "Content-Type: application/json  \r\n" +

    "Content-Length:22  \r\n" +

    " \r\n" +

    '{"BankCode": "900001"} ' + "\r\n" +

    " \r\n" +

    "--changeset_1a53241--  \r\n" +

    " \r\n" +

    "--batch_36522ad7-- ";

 

 

batchRequest.setBody(b2);

 

var response = client.request(batchRequest, b1slDestination).getResponse();

 

Instead of any useful things I allways get:

 

{

  "error" : {
  "code" : -1000,
  "message" : {
  "lang" : "en-us",
  "value" : "Incomplete Batch Request Body!"
  }
  }
}

 

The above code should instead insert a simple Banks - entity into Business One via the Odata Service Layer. The code it produces fits the OData spec and it works if I poste the exact same data via Chrome/ Postman e.g.:

 

POST: /b1s/v1/$batch

Header: Content-Type: multipart/mixed; boundary=batch_36522ad7

 

 

--batch_36522ad7

Content-Type: multipart/mixed; boundary=changeset_1a53241

 

--changeset_1a53241

Content-Type: application/http

Content-Transfer-Encoding: binary

 

POST /b1s/v1/Banks HTTP/1.1

Content-Type: application/json

Content-Length:22

 

 

{"BankCode": "9000010"}

 

 

--changeset_1a53241--

 

 

--batch_36522ad7--

 

the result then is:

 

--batchresponse_0f32bc7e-92a1-11e4-8000-0050562b029a

Content-Type:multipart/mixed;boundary=changesetresponse_0f32bd50-92a1-11e4-8000-0050562b029a

 

--changesetresponse_0f32bd50-92a1-11e4-8000-0050562b029a

Content-Type:application/http

Content-Transfer-Encoding:binary

 

HTTP/1.1 201 Created

Content-Type:application/json;odata=minimalmetadata;charset=utf-8

Content-Length:395

Location:http://192.168.135.221:50003/b1s/v1/Banks(4315)

 

{

   "odata.metadata" : "http://192.168.135.221:50003/b1s/v1/$metadata#Banks/@Element",

   "BankCode" : "9000010",

   "BankName" : null,

   "AccountforOutgoingChecks" : null,

   "BranchforOutgoingChecks" : null,

   "NextCheckNumber" : null,

   "SwiftNo" : null,

   "IBAN" : null,

   "CountryCode" : "DE",

   "PostOffice" : "tNO",

   "AbsoluteEntry" : "4315",

   "DefaultBankAccountKey" : null

}

--changesetresponse_0f32bd50-92a1-11e4-8000-0050562b029a--

--batchresponse_0f32bc7e-92a1-11e4-8000-0050562b029a--

 

Seeing a now successful insert;

 

So, I'm rather irritated what the difference in the above code is in comparision to the manual issued post. I tried to change the line carriages to different types, but there were no differences at all (so "\n" is as good as "\r\n"); Also I somehow suspect the XSJS  $.net.http.Request.setBody() method to maybe fiddle with the content, but I were unable to trace it any further as even the debugger held nearly no useful info for the Request object as it referes to 2 layers of proto's in the debug perspective;

 

Used Version of HANA is SPS07, B1 9.1 PL02;

 

Any help upon that matter would be reall appreciated.

 

Best Regards,

 

KB


Viewing all articles
Browse latest Browse all 6412

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>