Search code examples
iosobjective-cxcodensmutableurlrequest

NSMutableURLRequest not changing HTTPMethod


I have the following method in XCode:

- (void)makeRequest:(NSString *)wPin withMethod:(NSString *)HTTPMethod passValue:(NSString *)wVal {
    @try {

        NSString *params = @"";
        NSString *postString;
        NSData *postData;
        NSString *method;

        if ([HTTPMethod isEqualToString:@"GET"]) {
            params = [NSString stringWithFormat:@"?pin=%@", wPin];
            method = @"Retrieving data from";
        } else {
            postString = [NSString stringWithFormat:@"pin=%@&val=%@", wPin, wVal];
            postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
            method = [NSString stringWithFormat:@"Passing '%@' to", postString];
        }

        NSString *requestURL = [NSString stringWithFormat:@"%@%@", homeURL, params];

        NSLog(@"%@ '%@'", method, requestURL);

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestURL] cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:0];
        NSString *postLength = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)[postData length]];

        [request setHTTPMethod:HTTPMethod];

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

        [request setHTTPBody:postData];

        NSString *postBody = [[NSString alloc] initWithBytes: [request.HTTPBody bytes] length: [request.HTTPBody length] encoding:NSUTF8StringEncoding];

        NSLog(@"%@", postBody);

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate: self];

        if (conn) { //Makes connection
        }
    }
    @catch (NSException *e) {
        NSLog(@"Exception %@", e);
    }

}

When I call this method using:

[self makeRequest:wPin withMethod:@"GET" passValue:@""];

My server logs a healthy GET request, and sends me back the right data, and I parse it... it works, no problem.

However, when I call this method using:

[self makeRequest:wPin withMethod:@"POST" passValue:wState];

My server still logs the request method as "GET". I've even tried setting the HTTPMethod directly:

[request setHTTPMethod:@"POST"];

And my server still logs a "GET" request.

FYI: I developed the same app in JavaScript, and everything works fine (Server handles GET and POST correctly).

Here is my app console output:

(on load)

2015-01-28 10:25:08.324 Switch[2977:132599] Retrieving data from '<my server>?pin=18'
2015-01-28 10:25:08.330 Switch[2977:132599] Retrieving data from '<my server>?pin=23'
2015-01-28 10:25:08.611 Switch[2977:132599] 18OFF
2015-01-28 10:25:08.626 Switch[2977:132599] 23ON

(when I send three POST requests)

2015-01-28 10:25:28.842 Switch[2977:132599] Passing 'pin=18&val=1' to '<my server>'
2015-01-28 10:25:28.843 Switch[2977:132599] pin=18&val=1
2015-01-28 10:28:12.189 Switch[2977:132599] Passing 'pin=18&val=0' to '<my server>'
2015-01-28 10:28:12.190 Switch[2977:132599] pin=18&val=0
2015-01-28 10:28:13.702 Switch[2977:132599] Passing 'pin=23&val=0' to '<my server>'
2015-01-28 10:28:13.703 Switch[2977:132599] pin=23&val=0

Also, when you see something like "pin=18&val=1" in the output, note that's just an NSLog of the postBody before it sends. That's not a server response, unlike the "18OFF" you see in the output.


Solution

  • The problem was with the way the DNS works with my server. I have to circumvent port 80, so when my domain name is pointed to it's proper port to reach my router, I receive an "Object Moved" response from POST requests unless I enter my server's IP manually. This somehow means the POST request becomes a GET request before reaching my page. In using direct IP, the app works. I'll have to sort this issue out elsewhere. Thanks to all who tried helping!

    (The good news is, NSMutableURLRequest works!)