ios - How to add Authorization in Header for Get method in objective c -
i want add token in authorization header
nsurl *url2 = @"http://54.149.31.77:3000/api/search?number=98745612661"; nslog(@"%@ urlis ====>",urlstring); nsdata *data = [nsdata datawithcontentsofurl:url2]; nsstring *ret = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nsdata *dataaa = [ret datausingencoding:nsutf8stringencoding]; id json = [nsjsonserialization jsonobjectwithdata:dataaa options:0 error:nil]; nsarray * resultdict =[json objectforkey:@"name"];
you can use nsmutablerequest -
nsurl *url2 = @"http://54.149.31.77:3000/api/search?number=98745612661"; nsurlsession *session = [nsurlsession sharedsession]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:url2]; [request sethttpmethod:@"get"]; [request setvalue:[[nsuserdefaults standarduserdefaults] valueforkey:@"token"] forhttpheaderfield:@"authorization"]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; // add additional headers or parameters nsurlsessiondatatask *downloadtask = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { if (!error) { // response handling id json = [nsjsonserialization jsonobjectwithdata:data options:0 error:nil]; nsarray * resultdict =[json objectforkey:@"name"]; } }]; [downloadtask resume];
to understand in more detail nsurlsession refer tutorial
Comments
Post a Comment