John
John

Reputation: 5905

Post request from iPhone using ASIFormDataRequest not working

Newbie to Rails/iOS here. I have a rails blog application. I'm trying to upload a post from iOS using an HTTP POST method with ASIFormDataRequest. Here's the code that get's called:

    NSURL *url=[[NSURL alloc] initWithString:@"http://localhost:3000/posts"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"name"];

    [request setFile:@"star.png" forKey:@"image"]; 
    [request startSynchronous];  
    NSString *response = [request responseString];
    NSLog(@"response:: %@", response);

When I run this code, nothing happens. My server does not get contacted. I get response:: (null). Any idea why?

EDIT I found out that star.png needed to have its full file address. Now the POST works fine, but neither the name or image get saved into the db. A new post with empty name and image gets created. Any idea why?

Upvotes: 1

Views: 1288

Answers (2)

Alex Terente
Alex Terente

Reputation: 12036

NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSURL *url=[[NSURL alloc] initWithString:@"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"name"];

[request setPostValue:imageData forKey:@"image"]; 
[request startSynchronous];

For the "imagePath" is the path to your image. The full path.

Upvotes: 1

Mujah Maskey
Mujah Maskey

Reputation: 8804

why you are using localhost here?

NSURL *url=[[NSURL alloc] initWithString:@"http://localhost:3000/posts"];

use your web server ip instead of localhost

NSURL *url=[[NSURL alloc] initWithString:@"http://yourservername:3000/posts"];

Upvotes: 1

Related Questions