iphoneiosxmlpostkissxml

How to send POST XML from iOS app?


So, I've ran over and over the web in search for anything about sending XML with POST from iPhone app - no luck so far, none!

I'm using in my app KissXML, which I find very easy and useful when it comes to getting XML out of response - but quite opposite when sending XML to server... Here is my method for connecting and receiving XML. I tried to put NSString containing simply my XML request into body of POST request, but it doesn't work as planned.

-(void)prepareTransaction{
    NSLog(@"FXSecondVC: preparing transaction...");
    NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:@"kUsername"];
    NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"kPassword"];
    NSString *host = [[NSUserDefaults standardUserDefaults] stringForKey:@"kURLServer"];

    NSURL *url = [[NSURL alloc] initWithString:host];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    [httpClient setAuthorizationHeaderWithUsername:login password:password];
    [httpClient registerHTTPOperationClass:[AFKissXMLRequestOperation class]];

    NSString *xmlString = @"<RootEl xmlns=\"http://some.url/goes/here\">"
                                "<Element1>12678967.543233</Element1>"
                                "<Element2>"
                                    "<string xmlns=\"bla.bla/url\">"
                                            "String content</string>"
                                    "<string xmlns=\"bla.bla/url\">"
                                            "String content</string>"
                                "</Element2>"
                                "<Element3>true</Element3>"
                                "<Element4>String content</Element4>"
                                "<Element5>1999-05-31T11:20:00</Element5>"
                                "<Element6>true</Element6>"
                            "</RootEl>";

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:kServerRequestURL parameters:nil];
    [request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];

    AFKissXMLRequestOperation *operation = [AFKissXMLRequestOperation XMLDocumentRequestOperationWithRequest:request success:^(NSURLRequest *req, NSHTTPURLResponse *resp, DDXMLDocument *XMLDocument){
        NSLog(@"[SUCCESS]: XMLDocument: %@", XMLDocument);
    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, DDXMLDocument *XMLDocument) {
        NSLog(@"error parsing: %@", [error localizedDescription]);
    }];
    [operation start];
}

This is what I'm getting in response:

2012-11-21 19:40:09.884 FXApp[19662:707] FXSecondVC: preparing transaction...
2012-11-21 19:40:10.011 FXApp[19662:707] error parsing: Expected status code in (200-299), got 400

Am I missing something here? I want to use KissXML, because it the simplest way (at least known to me) to use already prepared XML document in successful response, but if solution requires changing framework - don't hesitate. The priority is to get it working. I hit dead end - this is driving me crazy, especially it is really urgent matter.


Solution

  • Mystery solved:
    it appears that all I had to do was to set Content-Type for xml - which I wasn't doing. Solution found here
    Here you go:

    [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];