Stephen
Stephen

Reputation: 4813

How can I confirm that an HTTP POST calls my code?

I'm trying to retrieve data from a POST method, code as follows:

-(void)postXMLFeed:(NSString *)XMLStrPost
{   
    //NSLog (@"XML Feed3: ", XMLStrPost);

    NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.x.xxx/stephen/sync_upload.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[XMLStrPost dataUsingEncoding:NSASCIIStringEncoding]];

    NSURLResponse *response;
    NSError *error;
    response = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


    // Update log file.
    NSLog(@"XML feed POSTED to website.");

    //[super postXMLFeed];
}

My XML feed is stored in the variable XMLStrPost. The above code seems to work but I have no way of confirming it.

The script should unparse the string and write it to a database, but this does not seem to be happening. Before the unparse takes place I want to confirm that script is being called.

How can I confirm that sync_upload.php script is called?

Upvotes: 1

Views: 256

Answers (4)

Dave DeLong
Dave DeLong

Reputation: 243156

You're misusing the API. +[NSURLConnection sendSynchronousRequest:returningResponse:error] returns an NSData object, which is the body of the response. You're assigning it into an NSURLResponse object, which means you're not only breaking the type system (you should be getting a warning when compiling), but you're also losing the pointer to the NSURLResponse that the connection gave you.

It's like you're shooting yourself twice in the foot with one bullet... ("Shooting two feet with one bullet"?)

Upvotes: 5

John Boker
John Boker

Reputation: 83719

Is it returning anything back to the device when it's done? I'd probably send back something that denotes success or failure from the script for you to read on the iPhone in the response variable.

Upvotes: 0

Dan Ray
Dan Ray

Reputation: 21903

Before you even get too deep here, let me seize the opportunity to recommend the All-Seeing Interactive HTTP library, at http://allseeing-i.com/ASIHTTPRequest/. Makes ALL your HTTP interactions vastly simpler.

That said, if you get your NSLog() call in the console, then the synchronous connection you're creating there has returned. You're not testing whether it returned with a failure of some type, though. If I were you, I'd NSLog myself some of the elements of your response object. But again, using ASIHTTP will allow you to ask the request object lots of good questions about its state.

Upvotes: 2

Kevin Panko
Kevin Panko

Reputation: 8534

Check if your log file shows an entry from the NSLog call.

Upvotes: 0

Related Questions