Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use ASI Http in iOS to POST data to a web service?

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!

like image 767
okysabeni Avatar asked Sep 01 '11 15:09

okysabeni


5 Answers

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:

In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application’s user interface will lock up and become unusable for the duration of the request.

like image 88
Suhail Patel Avatar answered Nov 19 '22 05:11

Suhail Patel


You forgot to call:

[request setDelegate: self];
[request startAsynchronous];

Or:

[request startSynchronous];

If you don't call any of these, the request will never be made :)

like image 40
Maurício Linhares Avatar answered Nov 19 '22 04:11

Maurício Linhares


I don't see a call to [request startSynchronous] (or [request startAsynchronous]) in your code... Are you even initiating the request anywhere?

like image 29
Mark Granoff Avatar answered Nov 19 '22 05:11

Mark Granoff


It looks like you didn't call [request startAsynchronous];

Check ASIHTTPRequest documentation for more details on how to use

like image 1
Zhao Xiang Avatar answered Nov 19 '22 04:11

Zhao Xiang


You're not actually sending the request.

1) You'll need to call [request startSynchronous] or [request startAsynchronous]

2) If you use asynchronous (which you should probably do) you'll need to set the delegate and implement a - (void)requestFinished:(ASIHTTPRequest *)request method.

like image 1
picciano Avatar answered Nov 19 '22 04:11

picciano