AFNetworking - 向REST服务发出XML POST请求?如何形成NSDictionary参数? [英] AFNetworking - Making an XML POST request to REST service? How to form NSDictionary parameters?

查看:144
本文介绍了AFNetworking - 向REST服务发出XML POST请求?如何形成NSDictionary参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为REST API创建POST请求以发布新的时间条目。 post请求所需的XML结构如下:

I am trying to create a POST request to a REST API to post a new time entry. The required XML structure for the post request is as follows:

<time-entry>
  <person-id>#{person-id}</person-id>
  <date>#{date}</date>
  <hours>#{hours}</hours>
  <description>#{description}</description>
</time-entry>

使用AFNetworking是在幕后完成的XML编码,只需将参数的NSDictionary传递到POST请求?这是我的代码到目前为止:

With AFNetworking is the XML encoding done behind the scenes just from passing an NSDictionary of parameters to the POST request? Here is my code so far:

从我的APIClient:

From my APIClient:

-(void)postTimeEntry:(TLActiveWork *)work {

[self setAuthorizationHeaderWithUsername:[self.activeUser username] 
   password:[self.activeUser password]];

// Most of the following is static data. Just trying to get a successful POST at the 
// moment.
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSString stringWithFormat:@"%@",[[self activeUser] personId]], @"person-id",
                        [NSString stringWithFormat:@"%@",[NSDate date]], @"date",
                        @"1", @"hours",
                        @"testing...!", @"description",
                        nil];

NSDictionary *wrapper = [NSDictionary dictionaryWithObjectsAndKeys:
                         params, @"time-entry",
                         nil];

 NSLog(@"Params: %@", wrapper);

// AFNetworking call!

[self postPath:[NSString stringWithFormat:@"/projects/%@/time_entries.xml",
   [[work project] projectId]] parameters:wrapper 
   success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"-----SUCCESS!!!-----");

} failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failure: %ld - %@", [operation.response statusCode], 
           [error description]);
}];
}

是的。我的APIClient操作通过以下方式为XML配置:

Yes. My APIClient operation(s) are configured for XML via:

[self registerHTTPOperationClass:[AFXMLRequestOperation class]];

现在每次我尝试用这个代码POST我总是得到422响应代码失败,相信意味着通过的实体是不可处理的。 http://stackoverflow.com/a/3291292/312411

Right now every time I try to POST with this code I always get a 422 response code failure which I believe means the entity passed was unprocessable. http://stackoverflow.com/a/3291292/312411

我认为我的问题是,这篇文章的XML没有通过NSDictionary params参数传递正确建立?

I think my problem is just that the XML for the post isn't being built correctly through the NSDictionary params argument passing ?

任何帮助将非常感激!

Any help would GREATLY be appreciated!

感谢。

推荐答案

希望它帮助别人遇到这个问题。下面是如何使用 AFNetworking 创建 XML POST 请求的示例。

While my answer is not timely, I hope it helps others that encounter this issue. Below is an example of how to make an XML POST request using AFNetworking.

AFHTTPClient *HTTPClient = [[AFHTTPClient alloc] initWithBaseURL:@"https://stackoverflow.com"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:HTTPClient.baseURL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSFileManager defaultManager] contentsAtPath:[[NSBundle mainBundle] pathForResource:@"TheNameOfTheXMLFileToSend" ofType:@"xml"]];

AFHTTPRequestOperation *operation = [HTTPClient HTTPRequestOperationWithRequest:request 
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
     // The server responded without an error.
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{
    // The server responded with an error.
}];

[HTTPClient enqueueHTTPRequestOperation:operation];

这篇关于AFNetworking - 向REST服务发出XML POST请求?如何形成NSDictionary参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆