2013-01-17 9 views
8

Sono appena iniziato con RestKit 0.20.0 e ho difficoltà a creare una richiesta JSON ben formattata.RestKit 0.20 L'oggetto JSON viene serializzato come richiesta stile GET nel corpo POST

ottengo questo (dai registri kit di riposo):

request.body=title=A%20glorious%20walk%20in%20the%20woods&startDateTime=2013-01-13%2016%3A09%3A33%20%2B0000&endDateTime=2013-01-13%2016%3A09%3A43%20%2B0000&points[][longitude]=-122.0307725&points[][latitude]=37.3310798&points[][longitude]=-122.0307334&points[][latitude]=37.33154242&points[][longitude]=-122.03075743&points[][latitude]=37.33138305&points[][longitude]=-122.03075659&points[][latitude]=37.33131185&points[][longitude]=-122.03057969&points[][latitude]=37.33156519&points[][longitude]=-122.03075535&points[][latitude]=37.33144466&points[][longitude]=-122.03076342&points[][latitude]=37.33123666&points[][longitude]=-122.03074488&points[][latitude]=37.33149482&points[][longitude]=-122.03068145&points[][latitude]=37.33155419&points[][longitude]=-122.03062909&points[][latitude]=37.33156564&points[][longitude]=-122.03076853&points[][latitude]=37.33115792 

quando voglio questo (normale oggetto JSON con parentesi graffe e una serie per la proprietà punti):

{ 
    title: "Something", 
    startDateTime: "dateinfo", 
    endDateTime: "moredateinfo", 
    points: [ 
     { 
      latitude: "37.33131313", 
      longitude: "122.4325454" 
     }, 
     { 
      latitude: "37.33131313", 
      longitude: "122.4325454" 
     } 
    ] 
} 

ho due oggetti principali: un DLWalk che contiene un NSSet di oggetti DLPoint (Sono oggetti CoreData, ma al momento lo ignoro e mi sto concentrando solo sulla creazione di una richiesta HTTP)

Ecco la c Ode sto usando per creare la mia richiesta:

// Point mapping 
RKObjectMapping *mappingPoint = [RKObjectMapping requestMapping]; 
[mappingPoint addAttributeMappingsFromArray:@[@"latitude", @"longitude"]]; 
RKRequestDescriptor *reqDescPoint = [RKRequestDescriptor requestDescriptorWithMapping:mappingPoint objectClass:[DLPoint class] rootKeyPath:nil]; 

// Walk mapping 
RKObjectMapping *mappingWalk = [RKObjectMapping requestMapping]; 
[mappingWalk addAttributeMappingsFromArray:@[@"endDateTime", @"startDateTime", @"title"]]; 
RKRequestDescriptor *reqDescWalk = [RKRequestDescriptor requestDescriptorWithMapping:mappingWalk objectClass:[DLWalk class] rootKeyPath:nil]; 


// Define the relationship mapping 
[mappingWalk addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"points" toKeyPath:@"points" withMapping:mappingPoint]]; 

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.10:8080"]]; 
          [manager addRequestDescriptor:reqDescWalk]; 
          [manager addRequestDescriptor:reqDescPoint]; 
          [manager addResponseDescriptor:responseDescriptor]; 

// POST to create 
[manager postObject:walk path:@"/walk/save" parameters:nil success:nil failure:nil]; 

Quindi la domanda è: perché non ricevo un oggetto normale cercando JSON nel mio corpo POST?

risposta

14

Quello che si ottiene come request.body è codificato tramite URL, che è RESTKit comportamento predefinito e di solito funziona bene.

Se si vuole che sia JSON-encoded, basta inserire questa riga prima di inviare la query

manager.requestSerializationMIMEType=RKMIMETypeJSON; 

Per ulteriori informazioni su questo, date un'occhiata là nella documentazione delle API per la classe RKObjectManager: requestSerializationMIMEType

+0

Così semplice, non posso credere di averlo perso! Grazie! – codemonkey

Problemi correlati