MKNetworkKit icon indicating copy to clipboard operation
MKNetworkKit copied to clipboard

Setting basic authentication header and content type header does not work when using "POST"

Open lanzi0907 opened this issue 10 years ago • 0 comments

Hi, here I met an issue that when I use http method 'POST', all the methods 'addHeader', 'setUsername' to adding basic authentication information and content type do not work. I print out the network operation, but all the headers I added are not seen.

For the http method 'GET', I invoked 'setUsername' to set basic username and password successfully. The printed out network operation includes the header. But when I use 'POST', the header does not exist in the printed out network operation.

Please help to check. Thank you!

  • (void)sendPostRequestWithJSON:(NSDictionary )paramDic secure:(BOOL)isSSL path:(NSString)reqPath responseCallback:(void (^)(NSData ))returnResponse { NSString paramJSONString = [paramDic jsonEncodedKeyValueString]; NSData* jsonData = [paramJSONString dataUsingEncoding:NSUTF8StringEncoding];

    MKNetworkOperation networkOperation = [self operationWithPath:reqPath params:nil httpMethod:@"POST"]; [networkOperation setUsername:AUTH_USERNAME password:AUTH_PASSWD basicAuth:YES]; [networkOperation addHeader:@"Authorization" withValue:AUTH_HEADER]; [networkOperation addHeader:@"Content-Type" withValue:@"application/json"]; [networkOperation setPostDataEncoding:MKNKPostDataEncodingTypeJSON]; [networkOperation setCustomPostDataEncodingHandler:^NSString(NSDictionary *postDataDict) { return paramJSONString; } forType:@""]; [networkOperation addData:jsonData forKey:@"json"];

    [networkOperation addCompletionHandler:^(MKNetworkOperation *completedOperation) { returnResponse([completedOperation responseData]);

    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) { NSLog(@"POST operation error data: %@", [error localizedDescription]); }]; NSLog(@"!!My request is: %@", networkOperation); [self enqueueOperation:networkOperation]; }

lanzi0907 avatar Aug 20 '14 14:08 lanzi0907