Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Problems with xhr.setRequestHeader() and content-type

I am trying to talk with an api over post and json. But its keep sending the content-type as
application/x-www-form-urlencoded; charset=utf-8

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function()
            Ti.API.info('in utf-8 onload for POST');
    xhr.onerror = function()
            Ti.API.info('in utf-8 error for POST');
    xhr.open("POST", "http://url.tld/api_jsonrpc.php");
    xhr.setRequestHeader("Content-Type", "application/json-rpc");

        'method': 'user.authenticate',
        'jsonrpc': '2.0',
        'params': {"user": "demo", "password": "demo"},
        'auth': false,
        'id' : 1

Do any have a reason for that?

— asked October 7th 2010 by Jesper Grann Laursen
  • content-type
  • http
  • json
  • mobile
  • request
  • xhr
  • Can you tell me does this worked for android app ?

    — commented November 21st 2012 by Vikas Gupta
  • Thanks a million times :):)

    — commented November 21st 2012 by Vikas Gupta
  • Can you please tell me how did you solve this issue as the accepted answer below of making the configuration related settings for XHR come before the open method is not working for me.

    All help will be much appreciated.


    — commented August 2nd 2013 by Bably Das

2 Answers

  • Accepted Answer

    Configuration related settings for XHR should come before the open method is called and this includes setting the timeout as well. If you try to do an open and then change the settings they will not apply to the connection that was previously been opened.

    This should work

        xhr.setRequestHeader("Content-Type", "application/json-rpc");
        xhr.open("POST", "http://url.tld/api_jsonrpc.php");
    — answered October 7th 2010 by John McKnight
    • BTW - If you are interested/bored and are building an iPhone project.. Open the XCode project in your build directory and look in Classes/Network/TiNetworkHTTPClientProxy.m around line 331 or so and you'll set the requestMethod is being set during the open method. Changing that and other values after the open has no effect.

      — commented October 7th 2010 by John McKnight
    • If you do this with Android then an error is fired. What's the deal?

      — commented September 10th 2012 by Tony Wright
    • @john i am also trying use a json-rpc web service and you told to set header before xhr.open statement, but i am getting this error
      setRequestHeader can only be called before invoking send.

      — commented November 21st 2012 by Vikas Gupta
    • Same issue as Vikas here. I can't get any of the examples of posting to a php script to work

      — commented November 27th 2012 by Abraham Brookes
    • The docs mention the opposite of this advice: "Sets the value for the specified request header. Must be called after open but before send."

      — commented December 29th 2012 by Francis Meetze
    • I just want to confirm that for me, setting the request headers should be after the open. If I set before then it doesn't work.

      — commented January 28th 2015 by Justin Toth
  • try xhr.setRequestHeader('Content-Type', 'application/json);

    — answered September 20th 2012 by Jyoti Gupta
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.