ASIFormDataRequest and appendPostString

325 views
Skip to first unread message

Daniel S.

unread,
Aug 12, 2010, 11:50:20 AM8/12/10
to ASIHTTPRequest
Hi,
First of all Great Work!!! This is very useful.

In my iPhone project I'm using ASIFormDataRequest to send a json
message by POST method, and I'm doing it like this:

[request setRequestMethod:@"POST"];
[request appendPostString: jsonMessage];

But I want to know if using the appendPostString method is the right
way to do it.
Because when I compile the project I have a warning in that very line
saying: "ASIFormDataRequest may not respond to '-appendPostString:' ".

Thanks.

Joseph Heenan

unread,
Aug 12, 2010, 2:55:26 PM8/12/10
to asihttp...@googlegroups.com

I'm not sure you need to use ASIFormDataRequest.

Here's what I do:


- (void)doRequest:(NSURL *)url params:(NSDictionary *)dict
{
NSString *postData = [dict JSONRepresentation];

request = [[ASIHTTPRequest requestWithURL:url] retain];
[request setDelegate:self];
[request addRequestHeader: @"Content-Type" value:
@"application/json; charset=utf-8"];
[request appendPostData:[postData
dataUsingEncoding:NSUTF8StringEncoding]];

[request startAsynchronous];
}


Joseph

Ben Copsey

unread,
Aug 16, 2010, 11:41:59 AM8/16/10
to asihttp...@googlegroups.com
Hi Daniel

ASIFormDataRequest's appendPostString: is a private method that is used to help record information about the request body when DEBUG_FORM_DATA_REQUEST is turned on. There shouldn't normally be any reason to use it yourself.

As Joseph says, you shouldn't use an ASIFormDataRequest for this - a regular ASIHTTPRequest as in his example would be the best way to go. ASIFormDataRequest is only used when you want to do a multipart/form-data or url-encoded form post. In your case, you simply want to send the JSON as the request body, and appendPostData: is the preferred way of doing that.

Best

Ben


Daniel S.

unread,
Aug 17, 2010, 6:00:35 AM8/17/10
to ASIHTTPRequest
Hi, thank you both for the response and help here.

I have another question:

At some point my app will use HTTPS to communicate with the server and
I want to know if do I need to change something in the request to make
it work.

Best regards,

Daniel

Joseph Heenan

unread,
Aug 17, 2010, 6:17:51 AM8/17/10
to asihttp...@googlegroups.com
Daniel S. wrote:
> Hi, thank you both for the response and help here.
>
> I have another question:
>
> At some point my app will use HTTPS to communicate with the server and
> I want to know if do I need to change something in the request to make
> it work.
>

It should just work if you change the url.

If you have a self-signed certificate (or one where the certificate
authority is not present on the iphone) you will need to [request
setValidatesSecureCertificate:NO]. If you are worried about your app
being attacked that may mean you also need to verify that the
certificate returned is the correct one.

Beware that https can be a huge additional performance penalty for small requests when running on a 2G/GSM mobile network, so you may want to consider only using it for really sensitive requests.


Joseph

Daniel S.

unread,
Aug 17, 2010, 11:24:26 AM8/17/10
to ASIHTTPRequest
Thanks Joseph.
Very useful info!

Redards,
Daniel
Reply all
Reply to author
Forward
0 new messages