mardi 4 août 2015

Afnetworking 2.0 POST text/plain converts characters

I’m using AfNetworking 2.0 in my iOS project, I have to make a API POST call to my API as text/plain whose body (operation.bodyTextPlain) is a string as this:

MSH|^~\&|DATA||DATA||20150717095405||ORU^R01^ORU_R01|

The problem is before to send the request, the content of the request.HTTPBody is a NSData as this (after convert to NSstring):

MSH%7C%5E\R\%5C%26%7CDATA%7C%7CDATA%7C%

I don’t need AfNetworking replaces the unsafe ASCII characters with “%” followed by two hexadecimal digits, because the response of the request is:

ERR|||207^Determine encoding for message. The following is the first 50 chars of the message for reference, although this may not be where the issue is: 
(null)=MSH%7C%5E\R\%5C%26%7CDATA%7C%7CDATA%7C%7C20^HL70357|E

So, I have the following code:

self.httpClient.requestSerializer = [AFHTTPRequestSerializer serializer];
[self.httpClient.requestSerializer setValue:@"text/plain; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[self.httpClient.requestSerializer setValue:@"text/plain" forHTTPHeaderField:@"Accept"];

[self.httpClient POST:operation.url parameters:operation.bodyTextPlain success:^(NSURLSessionDataTask *task, id responseObject) {

    if ([self isOKResponse:responseObject]) {
        [self operationResult:self.myObject error:nil];
    } else {
        [self operationResult:nil error:self.e];
    }

} failure:^(NSURLSessionDataTask *task, NSError *error) {
    [self operationResult:nil error:error];
}];
self.httpClient.requestSerializer = [AFJSONRequestSerializer serializer];

Really grateful for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire