I
I
Ivan Gnatyuk2015-01-25 12:46:37
In contact with
Ivan Gnatyuk, 2015-01-25 12:46:37

Sending a VK image with a POST request?

The VK documentation on posting an image on a wall says the following: Uploading
photos to a user's wall is carried out in 3 stages:
user_id or group_id parameters.
2. The application generates a POST request to the received address.
The request must include a photo field containing an image file (JPG, PNG, BMP or GIF)

I'm at the second stage, and obviously I can't send the image data correctly, because the response comes:

{"server":623218,"photo":"[]","hash":"72e9c4410f469435158d7557d46871c3"}

Tried many options, one of:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
    request.HTTPMethod = @"POST";
    
    if (params) {
        
        UIImage *yourImage= [UIImage imageNamed:@"share.png"];
        NSData *imageData = UIImagePNGRepresentation(yourImage);
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[imageData length]];
        
        // Init the URLRequest
        [request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:imageData];
    }
    else {
        [request addValue:@"text/xml; charset=utf-8; multipart/form-data;" forHTTPHeaderField:@"Content-Type"];
    }
    
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (connection) {
        NSLog(@"Connecting...");
        _receivedData = [[NSMutableData alloc] init];
    } else
    {
        // при попытке соединиться произошла ошибка
        NSLog(@"Connecting error");
    }

The main question is how to correctly include in the request a field with the name photo, containing a file with an image?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question