I am using the instructions provided in the ASI page here. I am trying to send some data to a web service and not seeing any results.
This is my sendRequest method which gets called in viewDidLoad
-(void)sendRequest {
NSURL *url = [NSURL URLWithString:@"http://153.60.6.75:8080/BarcodePayment/transactions"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
NSString *dataContent = @"{\"id\":7,\"amount\":7.0,\"paid\":true}";
NSLog(@"dataContent: %@", dataContent);
[request appendPostData:[dataContent dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:@"POST"];
}
I check the dataContent string and the output is
{"id":7,"amount":7.0,"paid":true}
If I use curl from Terminal, I checked and this command works.
curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://153.60.6.75:8080/BarcodePayment/transactions/ --data '{"id":7,"amount":7.0,"paid":true}'
My understanding is that in using curl, I set it to json, specify the address, specify the data which is equivalent to dataContent in my code. Nothing happens. What’s wrong?
Thanks for the help!
You have pretty much everything except the most crucial component which is to start the request
You need to add
[request startSynchronous];for a Synchronous Request or[requester startAsynchronous];for a Asynchronous request (and you possibly need the Delegate Methods to handle any response you have back)This is all covered pretty nicely in the ASIHTTPRequest How to Use Guide. The Sections most relevant to this would be ‘Creating a synchronous request’ and ‘Creating an asynchronous request’. Also something to think about taken from that page: