how to send the values in body when I call the asynchronous post request

52 views Asked by At

I am trying to do a task which I am completely not aware of, that is video uploading to server in objective c. I am a beginner and I was using swift, but now my requirement is in objective c.

Here I have to send an asynchronous request using multipart form data to server with three values. here is the data need to send in body:

body->form-data
projectId : String
sessionId : String
file : file

Its getting crashed at this line

" [urlRequest addValue:@"65" forHTTPHeaderField:@"projectId"];"

Can anyone help me to do this.

Thanks in advance.

NSString *str = [NSString stringWithFormat:@"http://abcdefghijkl.mnopqrst.com:8965/upload"];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[urlRequest addValue:@"65" forHTTPHeaderField:@"projectId"];
[urlRequest addValue:@"43" forHTTPHeaderField:@"sessionId"];
[urlRequest addValue:@"" forHTTPHeaderField:@"file"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error) {
        NSLog(@"Error,%@", [error localizedDescription]);
    } else {
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
    }
}];
1

There are 1 answers

1
Davydov Denis On

You should use NSMutableURLRequest

NSMutableURLRequest is a subclass of NSURLRequest that allows you to change the request’s properties.

NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:@"65" forHTTPHeaderField:@"projectId"];
...

request = [mutableRequest copy];