如何使用RestKit 0.20.1发送XML格式的POST请求

时间:2013-06-01 03:15:40

标签: ios xml http-post restkit-0.20

我有一个iOS应用程序,它使用RestKit 0.20.1从服务器提取数据。此时服务器可以发送JSON格式的数据,但无法接收JSON格式的数据。

这就是我的问题所在。 POST请求需要HTTP Body,服务器设置为接收XML。我已经实现了RKXMLReaderSerialization添加,所以我可以接收XML但是我找不到任何当前的方式来发送带有RestKit的XML格式的HTTP Body。

这个问题“Send post request in XML format using RestKit”正是我所寻找的,但Imran Raheem的答案现在(据我所知)由于RestKit的变化而过时。

我正在使用此方法进行POST

[[RKObjectManager sharedManager] postObject:nil path:@"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];

这是RestKit objectManager对postObject方法的描述

/**
 Creates an `RKObjectRequestOperation` with a `POST` request for the given object, and enqueues it to the manager's operation queue.
     @param object The object with which to construct the object request operation. If `nil`, then the path must be provided.
     @param path The path to be appended to the HTTP client's base URL and used as the request URL. If nil, the request URL will be obtained by consulting the router for a route registered for the given object's class and the `RKRequestMethodPOST` method.
     @param parameters The parameters to be reverse merged with the parameterization of the given object and set as the request body.
     @param success A block object to be executed when the object request operation finishes successfully. This block has no return value and takes two arguments: the created object request operation and the `RKMappingResult` object created by object mapping the response data of request.
     @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred.

如果我将MIMEType设置为JSON,则跟踪会显示我的request.body正在填充request.body={"Search":"Trending"}

但是,如果我将MIMEType设置为XML,则跟踪会显示request.body=(null)

以下是我用来更改MIMEType

的行
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

我是iOS和Objective-C的新手,所以我可能会设置在`postObject'方法的参数中使用的NSDictionary错误。这是以防万一......

NSArray *objects =[NSArray arrayWithObjects:@"trending", nil];
NSArray *keys =[NSArray arrayWithObjects: @"Search",nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
self.search=params;

任何帮助将不胜感激!鉴于我是新的Snippets特别有用!

哦,顺便说一下,如果有人能指出我接受JSON输入的REST方法,我很乐意将它传递给我的服务器人,所以我可以一起避免使用XML。

1 个答案:

答案 0 :(得分:1)

正如在本主题的其他帖子中所担心和建议的那样,RestKit 0.20.1版本不支持编写XML。以下是我与Blake Watters就此主题进行对话的链接。

https://github.com/RestKit/RestKit/issues/1430#issuecomment-19150316

当MIMEType设置为XML时,我从未明白为什么request.body =(null)。

服务器管理员同意将其设置为接收JSON。这就是我计划解决这个问题的方法。