Search code examples
iosobjective-ciphonecurlnsmutableurlrequest

How do I convert this curl command to Objective-C


the command:

curl -X POST -F "search={\"page\":\"1\",\"county\":\"18\",\"type\":\"J\"} " -H "Host: 118932.ro" http://118932.ro/dezv/ipa/index.php

This is what I have so far:

url = [NSURL URLWithString:@"http://www.118932.ro/dezv/ipa/index.php"];

NSData *data = [NSJSONSerialization dataWithJSONObject:self.requestData options:0 error:&error];
NSString *bodyString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSString *dataString = [NSString stringWithFormat:@"\"search=%@\"",bodyString];
NSData *dataBody = [dataString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:dataBody];
[request setValue:@"118932.ro" forHTTPHeaderField:@"Host"];

where self.requestData is the following dictionary

self.requestData = @{@"page" : @"1", @"county" : @"18", @"type" : @"J"};

but this doesnt seem to give me any results after I do this

self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[self.connection start];

Solution

  • Here is the code I use. The -F parameter in cUrl specifies the actual post data, so you may want to use the string "search={\"page\":\"1\",\"county\":\"18\",\"type\":\"J\"} " as is. You should call this code in a background thread as the synchronous call will block the UI.

    /**
     *  Do an HTTP POST request and return the results
     *
     *  @param  NSString *  url     the URL
     *  @param  NSString *  post    the POST data
     *
     *  @return NSDictionary *      a dictionary containing the response, error, and data
     */
    + (NSDictionary *)httpPostRequestWithUrl:(NSString *)url post:(NSString *)post
    {
        NSData *postData     = [post dataUsingEncoding:NSUTF8StringEncoding];
        NSString *postLength = [NSString stringWithFormat:@"%d", (int)[postData length]];
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        [request setTimeoutInterval:30]; // set timeout for 30 seconds
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
        NSHTTPURLResponse   *response = nil;
        NSError         *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        NSMutableDictionary *rc = [[NSMutableDictionary alloc] init];
        if ( response )
            [rc setObject:response forKey:@"response"];
        if ( error )
            [rc setObject:error forKey:@"error"];
        if ( data )
            [rc setObject:data forKey:@"data"];
    
        return (NSDictionary *)rc;
    }