2

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot CouchDeveloper. Your answer helped me a lot. The third point made the code to work. Thanks once again.
1

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.

2 Comments

Thanks for your reply. Still getting the same issue. I tried this code previously. Not able to find where the issue is.
Your code made my day. The Edit did all the work. I never thought about the position of the NSURLConnection. Somehow, it slipped from my mind.Thanks mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.