I've been trying to POST
to a REST
service a JSON object
that contains among other objects, an NSArray
of NSDictionaries
.
I've been doing this for other services but curiously enought none of them included an NSArray
of NSDictionaries
, so for those other cases it was pretty straight forward creating the needed JSON
s, but for this new one I thought it was just a matter of adding the NSArray
of NSDictionaries
but apparently not. This is how I create the original object in preparation to send it:
myDict = [NSDictionary dictionaryWithObjectsAndKeys:@"NFS", @"ticket[serie]",@"123",@"ticket[someID]",[NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:@"99SKU1", @"product_id",@"1",@"quantity",nil]],@"lineItems",nil];
Then, that dictionary I convert it to an NSData object in preparation to send it overa URLConnection
:
if ([NSJSONSerialization isValidJSONObject:myDict])
{
myData = [NSJSONSerialization dataWithJSONObject:myDict options:0 error:&theErrorIfAny];
// Send to webservice (myData)
}
I don't have access to Ruby code but I do have access to the log file that the server spits to a file and this is what I get:
{"ticket[serie]"=>"NFS"
"ticket[someID]"=>"123"
"lineItems"=>[{"product_id"=>"99SKU1","quantity"=>"1"}]
"ticket"=>{}}
Everything seems good except for that extra line: "ticket"=>{}
like if I'm filling the request with an extra empty dictionary. I've already tried this request by removing all but one item in myDict, removing even the nested NSArray
with NSDictionaries
in it, and I still see that "ticket"=>{}
line at the end of the request.
BTW, the webservice is being done in Ruby and I don't have direct access to it. I can comment on this to the people doing it but they just shrug their shoulders and argue that if I do the same call but from Terminal using curl -d... I'll get a correct response from the server. So maybe is something with JSONSerialization
object or the way I'm creating my call?:
+ (NSURLConnection *)postRequest:(NSData *)dataRequest toServerURL:(NSString *)serverURL onPort:(int)port withTimeOut:(NSTimeInterval)timeOut charSet:(char *)charset withDelegate:(id<NSURLConnectionDelegate>)myDelegate toServiceNamed:(NSString *)serviceName {
NSString *postDataLength = [NSString stringWithFormat:@"%d", [dataRequest length]];
DebugLog(@"bytes to send:%@", postDataLength);
NSMutableURLRequest *postRequest;
if (port == 0)
{
postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", serverURL, serviceName]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeOut];
}
else
{
postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@:%d/%@", serverURL, port, serviceName]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeOut];
}
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:postDataLength forHTTPHeaderField:@"Content-Length"];
if (charset == nil || strcmp(charset, ""))
{
charset = "utf-8";
}
[postRequest setValue:[NSString stringWithFormat:@"application/json; charset=%s", charset] forHTTPHeaderField:@"Content-Type"];
[postRequest setHTTPBody:dataRequest];
return [[NSURLConnection alloc] initWithRequest:postRequest delegate:myDelegate];
}
Any comment or point to some proven knowledge source would be great.