diff --git a/AFNetworking/AFURLRequestSerialization.m b/AFNetworking/AFURLRequestSerialization.m index c0a6618caf..9a2ac98092 100644 --- a/AFNetworking/AFURLRequestSerialization.m +++ b/AFNetworking/AFURLRequestSerialization.m @@ -500,7 +500,7 @@ - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request } if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { - if (query) { + if (query && query.length > 0) { mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query]]; } } else { diff --git a/Tests/Tests/AFHTTPRequestSerializationTests.m b/Tests/Tests/AFHTTPRequestSerializationTests.m index 1aedf72d4c..3c1765b9f4 100644 --- a/Tests/Tests/AFHTTPRequestSerializationTests.m +++ b/Tests/Tests/AFHTTPRequestSerializationTests.m @@ -100,6 +100,12 @@ - (void)testThatAFHTTPRequestSerialiationSerializesQueryParametersCorrectly { XCTAssertTrue([[[serializedRequest URL] query] isEqualToString:@"key=value"], @"Query parameters have not been serialized correctly (%@)", [[serializedRequest URL] query]); } +- (void)testThatEmptyDictionaryParametersAreProperlyEncoded { + NSURLRequest *originalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]]; + NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:originalRequest withParameters:@{} error:nil]; + XCTAssertFalse([serializedRequest.URL.absoluteString hasSuffix:@"?"]); +} + - (void)testThatAFHTTPRequestSerialiationSerializesURLEncodableQueryParametersCorrectly { NSURLRequest *originalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]]; NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:originalRequest withParameters:@{@"key":@" :#[]@!$&'()*+,;=/?"} error:nil];