5

Trying to post information for nested parameters to a rails app and having some trouble.

#pragma mark - Begin Network Operations
- (void)beginNetworkOperation {
    NSURL *requestURL = [NSURL URLWithString:[self retrieveURL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:requestURL];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    [request setShouldContinueWhenAppEntersBackground:YES];
#endif

    [request setRequestMethod:@"PUT"];

    [request addRequestHeader:@"Content-Type" value:@"application/json"];

    [request addPostValue:strClientId forKey:@"client_id"];
    [request addPostValue:strAccessToken forKey:@"access_token"];

    NSDictionary *assetDictionary = [NSDictionary dictionaryWithObject:self.tags forKey:@"tags"];
    [request addPostValue:assetDictionary forKey:@"asset"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startSynchronous];
}

self.tags is just a NSString with comma separated values, however once arriving at the rails server the tags parameter cannot be read (params[:asset][:tags]).

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Kyle
  • 1,662
  • 2
  • 21
  • 38

2 Answers2

2

Here is snippet of working code with JSONKit on iOS.

[request addRequestHeader:@"Content-Type" value:@"application/json"];

NSMutableDictionary *requestDict = [[NSMutableDictionary alloc] init];
[requestDict setObject:@"iSteve" forKey:@"UserId"];
[requestDict setObject:@"1" forKey:@"CompanyCode"];
[requestDict setObject:@"IN" forKey:@"LineOfBusiness"];
[requestDict setObject:@"M" forKey:@"LineOfBusinessClassification"];
[requestDict setObject:pricingVariablesListString forKey:@"CarQuoteString"];
[request appendPostData:[requestDict JSONData]];
Abizern
  • 146,289
  • 39
  • 203
  • 257
Sanjer
  • 152
  • 2
  • 9
  • In jQuery .appendPostData is not working in IE. I am using IE 10 is there any workaround for this? JavaScript runtime error: Object doesn't support property or method 'appendPostData' – Jaisankar Mar 02 '15 at 17:01
2

Try to pass you dictionary as a JSON string and not a dictionary object.

You can do that using iOS5 JSON library, or this one for more compatibility:

https://github.com/stig/json-framework

What I do is use appendPostData because I am pretty sure that setting the header (addRequestHeader) and using addPostValue are not compatible functions. Here is an example of my code:

ASIFormDataRequest *request;
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:myDictionaryToPassAsAnArgument]];

When you use appendPostData, you can't use any addPostValue. You have to put everything in the dictionary.

MatLecu
  • 953
  • 8
  • 14
  • I have tried this by doing [request addPostValue:[assetDictionary JSONRepresentation] forKey:@"asset"]; and I get the same error on the rails server "TypeError (can't convert Symbol to Integer)" which really seems to make no sense at all.... – Kyle Nov 04 '11 at 21:01
  • Maybe it is because of the comas in your string, because comas are the separator of JSON. Try to replace comas in your sting by something else to see if it works using – stringByReplacingOccurrencesOfString:withString: – MatLecu Nov 04 '11 at 21:17
  • My test data doesn't yet contain any commas. – Kyle Nov 05 '11 at 01:32
  • Is this the way that it should be done? Is the code example the correct implementation? – Kyle Nov 05 '11 at 01:32
  • unfortunately no, still unable. This seems like it should be possible to do. – Kyle Nov 07 '11 at 16:53
  • I forgot to tell that when you use appendPostData, you can't use any addPostValue. You have to put everything in the dictionary. – MatLecu Nov 07 '11 at 18:57
  • Unfortunately could not wait longer for an answer and changed the API. I will privately test this and see if the solution works. – Kyle Nov 08 '11 at 18:14
  • Finally got this to work, using the built in JSON serializer for iOS 5. – Kyle Dec 07 '11 at 16:04
  • How would you handle this on server side? Suppose I am using C# .NET, do I have to get this JSON in string object? How would I deserialize it? – Paresh Masani Apr 16 '12 at 12:56
  • It depends on your framework. In some you receive a string and deserializes it, in others it is directly given to you in language objects. I don't no C# or .NET at all so I don't know how it works. – MatLecu Apr 16 '12 at 19:37