English 中文(简体)
AFNetworking - Making an XML POST request to REST service? 如何形成NSDictionary参数?
原标题:AFNetworking - Making an XML POST request to REST service? How to form NSDictionary parameters?

我正试图提出PPOST请求,要求REST AP张贴新的入境时间。 员额请求所需的XML结构如下:

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

有了AFNetworking,XML在幕后进行编码,从通过一个参数的NSDictionary到POST的要求? 我的法典是:

我的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行动(s)通过:

[self registerHTTPOperationClass:[AFXMLRequestOperation class]];

我现在每次尝试采用该守则时,我总是收到422份回复守则的失败,我认为这意味着通过的实体是无法处理的。 https://stackoverflow.com/a/3291292/312411

我认为,我的问题是,通过“NSDictionary params”的论点正确建立该员额的XML吗?

任何帮助都值得赞赏。

感谢。

问题回答

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];




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签