I have an app which communicates with the server. I need to send a message id to the server. I am using NSMutableURL Request. However, I am getting a 405 error while submitting the request.
Below is the code
messageid=@"1234";
NSURL *aUrl = [NSURL URLWithString:@"http://myexample.com:8080/Padua/rest/messages/messagetodelete"];
NSMutableURLRequest *deleterequest = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:deleterequest
delegate:self];
[deleterequest setHTTPMethod:@"POST"];
[deleterequest setValue:[NSString stringWithFormat:@"%d", messageid.length] forHTTPHeaderField:@"Content-Length"];
[deleterequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postString = messageid;
[deleterequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[connection start];
The server side code is below
@POST
@Consumes("text/plain")
@Path("/messagetodelete")
public void messageToDelete(String messageid){
//code to delete the message in MongoDB
System.out.println("R u here for iphone??");
}
Is the code in the client end (iPhone ) correct. The POST consumes a plain text. I believe this is where I am going wrong.
Could anyone please guide me as how to achieve it and make the NSMutableURLRequest accepted by the server?