Skip to content

Commit 42f835f

Browse files
committed
+ added forced content type setting, when it's not the default value
1 parent b5afbf0 commit 42f835f

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

‎JSONModel/JSONModelNetworking/JSONHTTPClient.m‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,15 @@ +(NSData*)syncRequestDataFromURL:(NSURL*)url method:(NSString*)method requestBod
155155
cachePolicy: defaultCachePolicy
156156
timeoutInterval: defaultTimeoutInSeconds];
157157
[request setHTTPMethod:method];
158-
159-
if (bodyString) {
160-
[request addValue: [self contentTypeForRequestString: bodyString] forHTTPHeaderField:@"Content-type"];
158+
159+
if ([requestContentType isEqualToString:kContentTypeAutomatic]) {
160+
//automatic content type
161+
if (bodyString) {
162+
[request addValue: [self contentTypeForRequestString: bodyString] forHTTPHeaderField:@"Content-type"];
163+
}
164+
} else {
165+
//user set content type
166+
[request addValue: requestContentType forHTTPHeaderField:@"Content-type"];
161167
}
162168

163169
//add all the custom headers defined
@@ -170,6 +176,8 @@ +(NSData*)syncRequestDataFromURL:(NSURL*)url method:(NSString*)method requestBod
170176
[request addValue:headers[key] forHTTPHeaderField:key];
171177
}
172178

179+
NSLog(@"request headers: %@", [request allHTTPHeaderFields]);
180+
173181
if (bodyString) {
174182
//BODY params
175183
NSData* bodyData = [bodyString dataUsingEncoding:defaultTextEncoding];
@@ -225,7 +233,10 @@ +(NSData*)syncRequestDataFromURL:(NSURL*)url method:(NSString*)method params:(NS
225233
paramsString = [NSMutableString stringWithString:@""];
226234
for (NSString* key in [params allKeys]) {
227235
[paramsString appendFormat:@"%@=%@&", key, [self urlEncode:params[key]] ];
228-
}
236+
}
237+
if ([paramsString hasSuffix:@"&"]) {
238+
paramsString = [[NSMutableString alloc] initWithString: [paramsString substringToIndex: paramsString.length-1]];
239+
}
229240
}
230241

231242
//set the request params
@@ -260,11 +271,14 @@ +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method param
260271
if (cachedResult) {
261272
if (cachedResult.mustRevalidate==NO) {
262273
//return hard cached object
274+
NSLog(@"hard cached, no revalidate");
263275
if (completeBlock) completeBlock(cachedResult.object, nil);
264276
return;
265277
} else {
266278
//revalidate from server
279+
NSLog(@"soft cached, do revalidate");
267280
customHeaders = cachedResult.revalidateHeaders;
281+
NSLog(@"custom headers: %@", customHeaders);
268282
}
269283
}
270284
}
@@ -304,11 +318,12 @@ +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method param
304318

305319
if (isUsingJSONCache==YES && responseData == kUseCachedObjectResponse) {
306320
//check for not-modified response
307-
//return cached object
321+
NSLog(@"using the soft cached object");
308322
jsonObject = cachedResult.object;
309323

310324
} else if (error==nil) {
311325
//data fetched successfuly from the net
326+
NSLog(@"use server response");
312327
jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
313328
}
314329

@@ -319,6 +334,7 @@ +(void)JSONFromURLWithString:(NSString*)urlString method:(NSString*)method param
319334

320335
if (isUsingJSONCache==YES && jsonObject!=nil && error==nil && responseData != kUseCachedObjectResponse) {
321336
//successfull call, cache it
337+
NSLog(@"add server response to cache, etag: %@", etag);
322338
[[JSONCache sharedCache] addObject:jsonObject forMethod:method andParams:@[method, params?params:@{}, bodyString?bodyString:@""] etag: etag];
323339
}
324340
});

0 commit comments

Comments
 (0)