2016-05-12 11 views
0

我得到一個令牌數字表單登錄api響應,我需要發送這個令牌的url與http頭字段。請首先從我的登錄api中獲得我的回覆。如何在httpheader字段中傳遞令牌號碼?

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImpvaG4uc21pdGhAZ21haWwuY29tIiwiaWQiOiI1NzFkYzI3NmU0YjA1NjVmNTcwZjM2ZGQiLCJpYXQiOjE0NjMwMzY2Nzd9.3p2lXjOvQ-iIJZWr4GwuHCYf9VCDZbb3l9O1a8d7Eqs","data":{"name":"John Smith","role":"driver"},"message":"success"} 

現在我需要在另一個URL發送此托克和我在一個字符串保存它。請查看標題字段的代碼。

- (void)viewDidLoad { 
[super viewDidLoad]; 

token = [[NSUserDefaults standardUserDefaults]objectForKey:@"Accesstoken"]; 
NSLog(@"Accesstoken %@", token); 



NSURL *theURL = [NSURL URLWithString:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/:page"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f]; 

//Specify method of request(Get or Post) 
[theRequest setHTTPMethod:@"GET"]; 

//Pass some default parameter(like content-type etc.) 
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 

//Now pass your own parameter 

[theRequest setValue:token forHTTPHeaderField:@"Authorization"]; 
NSLog(@"req %@", theRequest); 
NSURLResponse *theResponse = NULL; 
NSError *theError = NULL; 
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError]; 

//Now you can create a NSDictionary with NSJSONSerialization 
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError]; 
NSLog(@"url to send request= %@",theURL); 
NSLog(@"%@",dataDictionaryResponse); 

} 
+0

什麼是您的令牌請求類型 –

+0

令牌請求類型是GET –

+0

是你的令牌值日誌 –

回答

0

嘗試一下本作的Get - >

NSString *stringURL=[NSString stringWithFormat:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/:page"]; 


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringURL]]; 

[request addValue:@"token" forHTTPHeaderField:@"Authorization"]; 
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:returnData 
                options:kNilOptions              error:nil]; 
NSLog(@"--->%@",son); 

後>

NSError *error; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil]; 
NSURL * url = [NSURL URLWithString:[ NSString stringWithFormat:@"http://qa.networc.in:1336/api/dispatcher/rideHistory/:page"]];; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:60.0]; 
[request addValue:@"token" forHTTPHeaderField:@"Authorization"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPMethod:@"POST"]; 

NSMutableDictionary *mapData = [[NSMutableDictionary alloc] init];// 
[mapData setValue:@"Anyname" forKey:@"useremail"]; 
[mapData setValue:@"Anypwd" forKey:@"password"]; 
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error]; 
[request setHTTPBody:postData]; 


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 

            { 

             if(error == nil) 
             { 
              NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 
              NSLog(@"Data>>>> = %@",text); 
             } 

            }]; 
[postDataTask resume]; 
+0

什麼應該是API密鑰在我的情況下,一個參數? –

+0

請檢查更新的代碼 –

+0

同樣的迴應bro –

相關問題