Archived Forum Post

Index of archived forum posts

Question:

POST request with body and QueryString

Mar 25 '14 at 11:40

I'm using CkoHTTP to send a POST request to a server. The problem is that I have to send a QueryString, a Body with a POST request. Is that possible? I'm using the following code but it seems that if there is a BODY only the BODY gets sent... if there is no BODY the QueryString "moves" into the body.

CkoHttp *http = [[CkoHttp alloc] init];
CkoHttpRequest *ckRequest = [[CkoHttpRequest alloc] init];
[ckRequest setHttpVerb:@"POST"];
[ckRequest SetFromUrl:@"http://www.someurl.com?CMD=READDATA"];
[ckRequest AddHeader:@"Connection" value:@"keep-alive"];
[ckRequest AddHeader:@"Accept" value:@"*/*"];
[ckRequest AddHeader:@"Accept-Encoding" value:@"gzip, deflate"];
[ckRequest AddHeader:@"User-Agent" value:_USER_AGENT];
[ckRequest AddHeader:@"PAR1" value:@"Value1"];
[ckRequest AddHeader:@"PAR2" value:@"Value2"];
[ckRequest LoadBodyFromBytes:HTTPBody];
//
[http setFetchFromCache:NO];
[http setConnectTimeout:[NSNumber numberWithInt:60]];      // 60 seconds
[http setReadTimeout:[NSNumber numberWithInt:15*60]];      // 15 minutes
//
CkoHttpResponse *ckResponse = [http SynchronousRequest:@"www.someurl.com" port:[NSNumber numberWithInt:80] ssl:NO req:ckRequest];

Is there a way of sending BODY parameters AND the QueryString part if the method is a POST?

Thanks.


Accepted Answer

I'm sorry, I was mistaken. I think the solution is to use SynchronousRequest, but to not use HttpRequest.SetFromUrl. Instead, set the HttpRequest.Path explicitly to "/?CMD=READDATA".


Answer

Try using the PostUrlEncoded method instead, which allows you to specify the URL directly in the method arguments.


Answer

Unfortunately it does not work... The PostUrlEncoded sends only the parameters in the body (as expected) but it does not send the QueryString part (that I need).


Answer

Perfect! Now it works and it sends the QueryString even with post... I had to write this:

if (MyURL.query && MyURL.query.length > 0)
  [ckRequest setPath:[NSString stringWithFormat:@"%@?%@", MyURL.path, MyURL.query]];
else
  [ckRequest setPath:MyURL.path];

and now it works. Thanks!