Search code examples
c#restpostappcelerator

Can't update a custom object in Appcelerator Cloud Service, got an error: (401) Unauthorized


I'm working on a C# application that connects to the Appcelerator Cloud Service, so far I can make queries and login, but when I tried to update a Custom Object I got the following error: The remote server returned an error: (401) Unauthorized. This is my code for my POST request:

           url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&noDisponibles=" + noDisponibles;
            wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
            wrGetUrl.Method = "POST";
            wrGetUrl.ContentType = "application/json";
            objStream = wrGetUrl.GetResponse().GetResponseStream(); // this is the line where the error is thrown
            reader = new StreamReader(objStream);

I have a theory and it involves the login, at the beginning of my application I make a login which return an ok status, but I think I need to somehow let know ACS I logged in already by sending the session id or something like that when I try to update the Custom Object.

Edit That is why I tried to add the cookie in the header like this:

                url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&noDisponibles=" + noDisponibles;
                wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
                wrGetUrl.Method = "POST";
                wrGetUrl.ContentType = "application/json";
                wrGetUrl.Headers.Add("Set-Cookie", "_session_id=" + session + "; path=/; HttpOnly");
                objStream = wrGetUrl.GetResponse().GetResponseStream(); // this is the line where the error is thrown
                reader = new StreamReader(objStream);

Where session is the session id I got from my successful login. But even though I added this to the headers in my request the error The remote server returned an error: (401) Unauthorized.

Edit: I tried something else:

                url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&noDisponibles=" + noDisponibles;
                wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
                wrGetUrl.Method = "POST";
                wrGetUrl.ContentType = "application/json";
                wrGetUrl.Headers.Add("Cookie", session); //changed the header but it didn't work
                objStream = wrGetUrl.GetResponse().GetResponseStream(); // still throws the same error
                reader = new StreamReader(objStream);

How can I solve this problem? Thanks for any help in advance.


Solution

  • I solved the problem like this:

                    String fields = "fields={\"noDisponibles\":" + noDisponibles +"}";
    
                    String url = "https://api.cloud.appcelerator.com/v1/objects/Reservacion/update.json?key=appkey&id=" + idReservacion + "&_session_id="+session; 
                    HttpWebRequest wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
    
                    wrGetUrl.Method = "PUT";
                    wrGetUrl.ContentLength = fields.Length;
                    wrGetUrl.ContentType = "application/x-www-form-urlencoded";
    
                    Stream dataStream = wrGetUrl.GetRequestStream();
                    byte[] byteArray = Encoding.ASCII.GetBytes(fields);
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Close();
    
    
                    Stream objStream = wrGetUrl.GetResponse().GetResponseStream();
                    StreamReader reader = new StreamReader(objStream);
    

    I had to write in the DataStream of my HttpWebRequest to send the fields I wanted to update. Hope this may help people in the future.