Timothy Rajan
Timothy Rajan

Reputation: 1957

Getting 405 error in POST NSMutableURLRequest

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?

Upvotes: 2

Views: 1119

Answers (2)

CouchDeveloper
CouchDeveloper

Reputation: 19098

There are these potential issues:

  • A "delete" request should use the DELETE method.

  • The definition of the header
    [deleterequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    is wrong. A MIME type application/x-www-form-urlencoded does not have a parameter, and no charset. A server will ignore this parameter.

    The correct way to specify the header is:

    [deleterequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    

    and use UTF-8 as the character encoding for parameter strings before applying the percent encoding.

  • When setting the Content-Length header you need to correctly evaluate the length of the body data in bytes. In your code you are setting the number of UTF-16 code units of the string. So, in order to fix that, you would need to do the following:

    NSString* postString = ...;
    NSString* postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
    [deleterequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
    [deleterequest setHTTPBody: postData];
    
  • You need to ensure that parameter strings have been properly encoded.

Upvotes: 1

hunyadym
hunyadym

Reputation: 2243

If you accept text/plain on server side, then you have to send the POST request also with content type text/plain. Also, you are missing the semicolon from the Content-type string.

Try to change

[deleterequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

into

[deleterequest setValue:@"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

EDIT: You also need to move the part where you set the request parameters before the creation of NSURLConnection, because the request is deep-copied, changes on the request after creating the connection has no effect.

Upvotes: 1

Related Questions