406 - HTTP/1.1 406 Not Acceptable on Web Service POST request

You must Login before you can answer or comment on any questions.

I have a real problem. I'm trying to post data back to a web service. The exact same request works when it is made via a web browser but when I make the call in Titanium I get the error:

406 - HTTP/1.1 406 Not Acceptable
This is my code:
var xhr = Ti.Network.createHTTPClient({
        enableKeepAlive : false
});
 
xhr.open('POST', myURL);
 
var contentType = "application/json";
 
// app.globals.token contains my athorization token
xhr.setRequestHeader("Authorization", app.globals.token);
xhr.setRequestHeader("Accept", contentType);
xhr.setRequestHeader("Content-Type", contentType);
 
var _pData = {
    "__Type" : "UserDetails",
    "Forename" : "John",
    "Surname" : "Smith",
    "DateOfBirth" : "01/03/1988",
    "Email" : "john@johnsmith.com",
    "Title":"Mr",
    "MiddleNames":"Arthur",
    "Gender":"M",
    "NationalInsuranceNo":"JM12345D",
    "MobilePhoneNo":"07970 111 222",
    "Username":"jsmith",
    "Password":"pa$$word"
};
 
xhr.send(_pData);
I'm wondering if it has something to do with setting the "Accept" header because if I remove this the error changes to Error 500 Server Error. However I need to set this and the vendors of the web service have tested out the example above on my web server and it works for them. So is there something in the above code that Titanium does not like? I'd be very grateful if anyone was to help!

Richard

— asked 2 years ago by Richard Shergold
4 Comments
  • build your data as a string in the url open... and declare your headers before the open.

    — commented 2 years ago by Trevor Ward

  • Hi Trevor, when you say build the data as a string in the url open, what do you mean? The data is always passed as a paramater to the xhr.send isn't it ?

    — commented 2 years ago by Richard Shergold

  • I switched the headers to before the Open but I get an error of "Not Authorized" which implies that the Authorization header is not being set I think if I do it that way round.

    — commented 2 years ago by Richard Shergold

  • Show 1 more comment

3 Answers

hi Richard,

try to set this

var contentType = 'application/json;  charset=utf-8';

— answered 2 years ago by Mitul Bhalia
answer permalink
4 Comments
  • Hi Mitul. Thanks but it had no affect I'm afraid. Still get the 406 error.

    — commented 2 years ago by Richard Shergold

  • ok you want json response right...?

    so when you pass your dictionary as json format in .send() method then it returns as json so comment xhr.setRequestHeader("Accept", contentType); this line and test it again

    — commented 2 years ago by Mitul Bhalia

  • when I try that I get a different error

    HTTP/1.1 500 Internal Server Error
    I will get the server Admin to check the logs but presumably that error is the result of me passing JSON across to the Web Service and not spefifying in the header that it will be accepting JSON ?????

    — commented 2 years ago by Richard Shergold

  • Show 1 more comment

try this

xhr.setRequestHeader("Accept", 'text/plain, text/json');

— answered 2 years ago by Mitul Bhalia
answer permalink
4 Comments
  • or

    xhr.setRequestHeader("Accept", 'text/json');

    — commented 2 years ago by Mitul Bhalia

  • Hi - tried both. Still get the 406 Not Acceptable error.

    — commented 2 years ago by Richard Shergold

  • try to pass this json with the 'text/json' as Accept header

    var _pData = {  
        Forename : "John",
        Surname : "Smith",
        DateOfBirth : "01/03/1988",
        Email : "john@johnsmith.com",
        Title : "Mr",
        MiddleNames : "Arthur",
        Gender : "M",
        NationalInsuranceNo : "JM12345D",
        MobilePhoneNo : "07970 111 222",
        Username : "jsmith",
        Password : "password"
    };

    — commented 2 years ago by Mitul Bhalia

  • Show 1 more comment

Trevor and Mitul

Thanks for looking at this with me - much appreciated.

I got it to work with a very simple change.

xhr.send(JSON.stringify(_pData));
thanks again, Richard

— answered 2 years ago by Richard Shergold
answer permalink
1 Comment
  • oh.... very small mistake..

    anyway the point is you solved problem yourself....that is much appreciated you

    good luck

    — commented 2 years ago by Mitul Bhalia

Your Answer

Think you can help? Login to answer this question!