ATOM PUT Request

Apr 26, 2012 at 12:24 PM

Hi there,

I am wondering how to influence the data.js parser in order to gain my targeted body I would like to send with a PUT request.
In summary I would like to achive the following:
1) Set "atom:" in front of the tags "entry" and "content"
2) In the entry element, set additional properties like xml:base=<service_document_uri>
3) In the entry element, set xmlns:atom="" instead of xmlns=""
4) In the entry element, add a another entry: xmnls:xyz="my_uri"

Here is my current body output:

<entry xmlns=""
    <content type="application/xml">

Here the target body structure I would like to create:

<atom:entry xmlns:atom="" xmlns:d=""
    <atom:content type="application/xml">

If there is no standard way to achieve the above: Is there a way to construct the body entirely myself and pass it to the OData.request? I have tried it but could not figure out in which format it could work.

Thanks a ton in advance.

Apr 26, 2012 at 10:25 PM

After getting some help and debugging the data.js I would like to share my findings, hoping to narrow down the issue and to find a solution.

in data.js (I am using data-1.0.2.js) I checked the request which is acutally sent --> line 1070: xhr.send(request.body);
The request still fails and I do not understand why.

The reason why I am baffled is when I copy the request and put it in a REST client (FF) the request actually gets processed (return code 204).
I made sure that the headers, body, uri and PUT request is identical.

Any ideas?

Apr 27, 2012 at 12:46 AM


    First things first, can you share a network trace of the request and response using datajs so I can debug it. Thanks for bringing that to our attention :).  Now, on to the massaging of the payload.  Datajs allows you to inject your own payload handler if you need to do very specific formatting of the payloads. A handler object is an object that adheres to this contract:

var myHandler = {  
    read: function (response, context) {
        /// <summary>Reads the body of the response and saves it to</summary>
        /// <param name="response">Response object.</param>
        /// <param name="context">Operation context.</param>
        = doSomethingWithTheBody(response.body);

    write: function (request, context) {
        /// <summary>Write the body of the specified request from</summary>
        /// <param name="request">Request object.</param>
        /// <param name="context">Operation context.</param>
        response.body = doSomethingWithTheData(;       
    accept: "application/atom+xml",  // mime type that this handler accepts.
    maxDataServiceVersion: "2.0" // max version of the protocol supported by this handler.

  An then use it this way:  

    function(data, response) { 
        // success function 
    function(err) { 
        //error function 

  Or make it the default handler altogether:      

var oldDefaultHandler = OData.defaultHandler; //save it if it needs to be used later in the web app life.
OData.defaultHandler = myHandler;

  So you can create your own handler from the current atomHandler code (at least for the write function) that fine tunes the format as your scenario requires. You can also have your custom handler wrap the atomHandler itself and then fine tune the payload using some string transformations... Adding the atom: alias should be easy with string replace.  Injecting custom nodes to the xml itself might be trickier as you would have to parse again the XML, modify the try and serliazle it back to a string.


Alex Trigo.

May 2, 2012 at 7:39 PM
Edited May 2, 2012 at 7:40 PM

Hi Alex,

I found the issue. Your hint to provide a network trace let me find the issue.
The issue was that data.js implicitely set DataServiceVersion = 1.0 which my ajax call did not. I guess the server only accepts version 2.0...
Setting the request header to DataServiceVersion = 2.0 solved the issue. I will now do some more reading to understand the difference between 1.0 and 2.0 :)

Thanks for you quick help which lead me to fixing the problem.


May 2, 2012 at 10:30 PM

Hi Jan,

    Good you solved your issue.  The DataServiceVersion header indicates which version of the protocol the payload in the request / response adheres to.  Datajs will infer the value it should use based on what it finds in the payload and the format being used.  Do you know which server are you using? Was it configured to explicitly accept requests from  V2 clients?


Alex Trigo.