Ye Myat Min
Ye Myat Min

Reputation: 1429

Consuming RESTful services from Titanium

Is there anyway to do HTTP PUT/DELETE methods from Titanium? Are they supported? Is there any third-party library or workarounds that deals with it?

Upvotes: 2

Views: 1955

Answers (2)

Kuroro
Kuroro

Reputation: 1931

Yes, you can send PUT/DELETE as verb in HTTPClient

var url = "http://www.appcelerator.com";
 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: " + this.responseText);
         alert('success');
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         alert('error');
     },
     timeout : 5000  /* in milliseconds */
 });


 // Prepare the connection.
 // This accepts PUT/DELETE/GET/POST
 client.open("PUT", url);
 // Send the request.
 client.send();

Upvotes: 2

Loren Paulsen
Loren Paulsen

Reputation: 9388

Be aware that Titanium (HttpClient) will automatically convert a DELETE into a POST if you try to add parameters to the body, and will not give any clear indication this has happened. Parameters on a DELETE must be passed in the query string.

The code that performs this transformation is inside ASIHTTPRequest.m

if ([self postLength] > 0) {
    if ([requestMethod isEqualToString:@"GET"] || [requestMethod isEqualToString:@"DELETE"] || [requestMethod isEqualToString:@"HEAD"]) {
        [self setRequestMethod:@"POST"];
    }
    [self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",[self postLength]]];
}
[self setHaveBuiltPostBody:YES];

Source: http://developer.appcelerator.com/question/123042/only-get-and-post-methods-in-httpclient--no-put-or-delete

Upvotes: 0

Related Questions