Do a simple json POST using RESTKit

后端 未结 2 395
一个人的身影
一个人的身影 2020-12-09 04:04

I\'m new to iOS development and I\'m having trouble making a simple Json POST request. I have a NSDictionary containing an user and password and I want to send those values

相关标签:
2条回答
  • 2020-12-09 04:35

    I had the same problem, and this is what solved it for me. Note, in my scenario, I only wanted to access RKRequest.

    NSDictionarty *dict
    NSError *error; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
    
    if (!jsonData) 
    {
        NSAssert(FALSE, @"Unable to serialize JSON from NSDict to NSData"); 
    } 
    else 
    {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        request.params = [RKRequestSerialization serializationWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON];
    }
    

    The key for me was the 'MIMEType:RKMIMETypeJSON' in the last line. Since I only wanted to use RKRequest, this was how I needed to set the MIME type. Otherwise, I would use Paul Cezanne's suggestion.

    0 讨论(0)
  • 2020-12-09 04:54

    For older RestKit

    You probably have something like this in your delegate:

        objectManager.serializationMIMEType = RKMIMETypeFormURLEncoded;
    

    You want it to be:

        objectManager.serializationMIMEType = RKMIMETypeJSON;
    

    For RestKit v.20:

        // thanks  ColdLogic (from his comment)
        [objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
    
    0 讨论(0)
提交回复
热议问题