When i am trying to make a post request without (www-authorization) with NSURLSession and dataTaskWithRequest (i am using custom delegate).
It works well. (i send the request, on the server i just print echo $_POST['name'] and i get the "name" as the response.)
When i am trying to make a post request with (www-authorization) it looks like it works fine
If i enter the correct username and password i get the header with the status-code 200 and json data, if i make a mistake i get the header with the status code 401. On the server side i made
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Realm"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
else
{
$user = new btuser($_SERVER["PHP_AUTH_USER"],$_SERVER['PHP_AUTH_PW']);
if ($user->logToDB())
{
header('content-type:application/json');
header('test-login: '.$user->getname());
$array = array("fff"=>"tttt",
"ddd"=>"qqqq",
"uuuu"=>$_post['name']
);
echo json_encode($array);
}
else
{
header('HTTP/1.0 401 Unauthorized');
unset($_SERVER['PHP_AUTH_USER']);
}
}
, but "post" request is lost somewhere )))... I got something like this - {"fff":"tttt","ddd":"qqqq","uuuu":null}.
All delegete's methods looks like work fine ... the first method is
1.
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData: (int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend: (int64_t)totalBytesExpectedToSend{
NSLog(@"did send body data");
}
2.
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
NSLog(@"challenge");
NSURLCredential* newCredintioal;
newCredintioal = [NSURLCredential credentialWithUser:@"testuser" password:@"testpassword" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredintioal forAuthenticationChallenge:challenge];
completionHandler(NSURLSessionAuthChallengeUseCredential,newCredintioal);
}
3 Then again didSendBodyData.
4
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"did receive response %@", response);
completionHandler(NSURLSessionResponseAllow);
NSURLRequest* req = dataTask.currentRequest;
NSData* dt = [req HTTPBody];
NSString* body = [[NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
completionHandler(NSURLSessionResponseAllow);
NSLog(@"credintials %d \n %@ \n %@ \n %@ ",dataTask.state, body, dataTask, req);
}
5
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSError *jsonParsingError = nil;
NSDictionary *urlResponseDataDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
[_savedData appendData:data];
// NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"did receive data %@",urlResponseDataDict);
}
6
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
// NSError *jsonParsingError = nil;
// NSDictionary *urlResponseDataDict = [NSJSONSerialization JSONObjectWithData:_savedData options:0 error:&jsonParsingError];
NSString* str = [[NSString alloc] initWithData:_savedData encoding:NSUTF8StringEncoding];
NSLog(@"did complete with error - %@, data - %@, _saveddata - %@", error, str, _savedData ); //, urlResponseDataDict);
}
How can i make a simple data post request with authorisation (URLSession:task:task didReceiveChallenge:)? What am i doing wrong?
P.S. Sorry for my english.
The problem is probably on the PHP script: $_post
is not the same as $_POST
(note the capitalization)