Archived Forum Post

Index of archived forum posts


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:@""];
[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:@"" 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?


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".


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


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).


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]];
  [ckRequest setPath:MyURL.path];

and now it works. Thanks!