PDA

View Full Version : default header for xml upload query



jamie.nicholson
20 Aug 2007, 12:40 AM
Hi,

I've been using ext for a short time but have found it really good and used the forums extensively for tutorial like information from Animal and Jack the experts :) thanks for your time and efforts.

I'm an Perl web framework Catalyst that using the REST, although strictly speaking I'm not a RESTafarian and have only been playing around with Catalyst, REST and ext js.

Links to REST and tutorial on the Catalyst REST implementation.

http://en.wikipedia.org/wiki/Representational_State_Transfer
http://www.catalystframework.org/calendar/2006/9

So setup Catalyst and started using REST and wanted to apply it to my web pages.
Heres an extract of my javascript



//Generate some xml from field namesand values fs = fieldset
//------------------------------------------------------------
var xmlData = '<data>';
fs.items.each(function(f){
xmlData = xmlData + '<' + f.getName()+'>' + f.getValue()+'</'+f.getName()+'>';
});
xmlData = xmlData + '</data>';
var connection = new Ext.data.Connection();

//From ext I initiate a POST connection and send the data
//------------------------------------------------------------
connection.request(
{url:'/activityCodeREST/activityDescription/'+Ext.get('activityId').getValue(),
method:'POST',xmlData:xmlData
});
}


The data is sent nicely and the webserver and catalyst should be happy although it expects only text/xml in the content header, otherwise it doesn't parse the xml, without code modification and extra configuration. So I tried altering the default header as per forum examples, firebug showed for normal GET requests the content-type was set however the POST method with the xmlData option configured meant the content-type was always appended to.



ext-yui-adapters.js
if(_3a.xmlData){CN.initHeader("Content-Type",......
yui-utlities.js
default_post_header:"application/x-www-form-urlencoded"


I've read the ext js code for the above and there is specific code to default the POST and configured xmlOption content-type and then only appended to it, when using defaultHeader in the connection configuration.

From reading previous posts and the code I assume that you want the POST option to Context type to contain urlencoded and text/xml and that this was a design decision. I've got this working now so I don't need a solution, however I'd be interested to hear your rationale on this.

hendricd
20 Aug 2007, 4:37 AM
I believe to accomplish that in Ext:



Ext.lib.Ajax.useDefaultHeader = false;
connection.request(
{url:'/activityCodeREST/activityDescription/'+Ext.get('activityId').getValue(),
method:'POST',xmlData:xmlData
});
}



then, you should see only one: 'Content-Type: text/xml' in the outbound Request

But, Ext.lib.Ajax is a singleton, so you must remember to toggle it back on for form-posts.

jamie.nicholson
23 Aug 2007, 7:07 AM
Tried the solution you provided Henrick however it doesn't work, you actually need to modify the Ext JS source to get it going. Of course this will mean every POST header will contain text/xml and you'd need to change it with every new version in a production app.

ext-yui-adapters.js - Remove the if statement to init the header
if(_3a.xmlData){CN.initHeader("Content-Type","text/xml",false);

yui-utlities.js - Modify header to be text/xml
default_post_header:"application/x-www-form-urlencoded"

As I stated earlied the POST method will always execute the default_post_header unless there is some way of turning that header off as well, I'm not entirely sure.

If I figure something out I'll be sure to post it here.

-------------------------------------------------
Jamie Nicholson
Ext User

hendricd
23 Aug 2007, 8:21 AM
Sorry,

Didn't know you were talking about the YUI adaptor. Yes, you'll have to do some more digging, although I think the Ext-based solution I posted before should work with the base-adapter only.

jamie.nicholson
23 Aug 2007, 11:44 PM
No worries, I'm still very much walking around in the dark here. I'll need to do some source diving to figure out how it all works, as I've pretty much adapted it from examples, tutorials and stuff I've read in the api or forums.

The YUI adapters might be required for Ext.data.Connection but I'm not entirely sure.

I maybe completely wrong, I'll take a dip in the codebase at some point and find out and repost :)

dotnetCarpenter
4 Oct 2007, 11:32 AM
Sorry,
Didn't know you were talking about the YUI adaptor. Yes, you'll have to do some more digging, although I think the Ext-based solution I posted before should work with the base-adapter only.

Unfortunately, no. And we're talking Ext 1.1.1! I thought this was solved after version 1.0 but sadly no... I will post a workaround shortly.
Update

Ext.lib.Ajax.request = Ext.lib.Ajax.request.createInterceptor(function(method, uri, cb, data, options){
this.defaultPostHeader = "application/json; charset=utf-8;";
});