Everything is fine with POST requests, but couldn't find a way to work with PUT or DELETE?
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] init];
[mutableRequest setURL:[NSURL URLWithString:[NSString
stringWithFormat:@"http://abc.com/update/27"]]];
[mutableRequest setHTTPMethod:@"PUT"];
[mutableRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[mutableRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[mutableRequest addValue:@"PUT" forHTTPHeaderField:@"X-HTTP-Method-Override"];
[mutableRequest addValue:sessionId forHTTPHeaderField:@"ZURMO_SESSION_ID"];
[mutableRequest addValue:token forHTTPHeaderField:@"ZURMO_TOKEN"];
[mutableRequest addValue:@"REST" forHTTPHeaderField:@"ZURMO_API_REQUEST_TYPE"];
NSString *postLength = [NSString stringWithFormat:@"%d",[postdata3 length]];
[mutableRequest addValue:postLength forHTTPHeaderField:@"Content-Length"];
[mutableRequest setHTTPBody:postdata3]
This is how server side(PHP) handles api calls, with PUT,GET,POST and DELETE. Any help would be appreciated thanks
public static function createApiCall($url, $method, $headers, $data = array())
{
if ($method == 'PUT')
{
$headers[] = 'X-HTTP-Method-Override: PUT'; //also tried this one to add header
}
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method)
{
case 'GET':
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($handle);
return $response;
}
It's hard to tell without knowing the explicit error.
The use of the PUT or DELETE is often implemented by "hidden parameters" called _method. It's not only specific for example a Spring MVC's tag library, but is also used by a few other client frameworks. Spring is just following the convention, such as it is.
In order to use this properly, you have to know how your backend is handling with PUT or DELETE requests.
In spring for example you need to define a filter (HiddenHttpMethodFilter, see javadoc), which turns the _method parameter into a "real" HTTP method representation in the HttpServletRequest. This is done as a filter to emphasise the fact the the lack of PUT and DELETE is a browser problem - the servlet API supports it just fine.