I am trying to upload a text using multi-part form encoding
in iOS.
What i Did :
I have tried via the info provided in the ios Upload Image and Text using HTTP POST.
Problem :
I put log in server side, And here is what did i receive from my IOS App.
No problem with key
Name. But, I receive the value
as null
"443" "POST" "/api/private/json/" "content=null&scope=null&action=null&documentname=null&apikey=null"
My code :
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject:@"XXXXX" forKey:@"apikey"];
[params setObject:@"DataAPI" forKey:@"scope"];
[params setObject:@"push" forKey:@"action"];
[params setObject:docName forKey:@"documentName"];
[params setObject:content forKey:@"content"];
[params setObject:authToken forKey:@"authtoken"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
NSString *boundary = @"V2ymHFg03ehbqgZCaKO6jy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in params) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSString *myString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"%@",myString);
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[request setURL:url];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);
I couldn't able to understand the issue, Whether it is from server side or from my code.
Thanks for sharing your answers.