GJain
GJain

Reputation: 5093

getting XML from NSData response of an NSURLSessionDataTask request

I send a NSURLSessionDataTask and I get NSData as response. It should contain XML in it. But I am not able to get it.

When I send a curl request on command line, it works as expected

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5
    if (!error) {
        NSLog(@"%@", [response description]);
        NSLog(@"%@", data);   // some data I can not understand.
    }
}];

Request:

{
    "Content-Type" = "application/xml; charset=\"utf-8\"";
}

Response

{ status code: 207, headers {
    "Accept-Ranges" = bytes;
    Connection = close;
    "Content-Length" = 1282;
    "Content-Type" = "text/xml";

Please provide pointers

Upvotes: 0

Views: 3171

Answers (1)

larva
larva

Reputation: 5148

text/xml is just XML string so convert response data to string by

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Your code will be:

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5
    if (!error) {
        NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", responseString);
    }
}];

Upvotes: 4

Related Questions