1

I am going to develop web services based on REST API. It is my first project of web services.

I have a question regarding web services-iPhone app.

Suppose when a user logged in using iPhone app his will get authentication token.

Using this token he/she wiil be authorized to use futher operation of app . I am planing about that iPhone app needs to be send hearders along with post/get request method with authentication key in herders.

Request looks like: www.mydomaoin.com

But to authorize on server I need to pass special parameters to Header of request:

Headers: X-Testing-Auth-Secret: kI7wGju76kjhJHGklk76
Headers: Content-Type : application/json

which will be used in controller (zend framework)

public function preDispatch()  
{
    $request = new Zend_Controller_Request_Http();
    $key = $request->getHeader('X-Testing-Auth-Secret');
    $type = $request->getHeader('Content-Type');
}

I don't know an iPhone capable for sending headers or not. As as in case of android.

I am a php developer

Please help me

SP Singh
  • 416
  • 4
  • 18
  • In Android header parameters can be passed, so definitely iPhone should be able to – Shaolin Feb 21 '13 at 05:35
  • Se this post, how to pass headers using iPhone http://stackoverflow.com/questions/5116192/how-to-set-http-header-fields-in-objective-c – Shaolin Feb 21 '13 at 05:37

5 Answers5

2

You can use the following code

NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    NSString *post = @"your json";
    //NSLog(@"%@",post);
    NSData *postdata = [NSData dataWithBytes:[post UTF8String] length:[post length] ];
    [req setHTTPMethod:@"POST"];
    [req setValue:@"application/json" forHTTPHeaderField:@"content-type"];
    [req setValue:@"kI7wGju76kjhJHGklk76" forHTTPHeaderField:@"Content-Length"];
    [req setValue:[NSString stringWithFormat:@"%d",[postdata length]] forHTTPHeaderField:@"X-Testing-Auth-Secret"];
    [req setHTTPBody:postdata];

    (void)[NSURLConnection connectionWithRequest:req delegate:self];
manujmv
  • 6,450
  • 1
  • 22
  • 35
2

There are some delegate methods you can use.

-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName``
  namespaceURI:(NSString *) namespaceURI
 qualifiedName:(NSString *) qName
   attributes:(NSDictionary *) attributeDict {

    if( [elementName isEqualToString:@"AddGeneralInformationResult"])
   {
    if (!soapResults)
    {
        soapResults = [[NSMutableString alloc] init];
    }
    elementFound = YES;
   }


    }

  -(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
   {
if (elementFound)
{
    [soapResults appendString: string];
}
}

-(void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
 {

 if ([elementName isEqualToString:@"AddGeneralInformationResult"])
 {
    elementFound = NO;
    //NSLog(@"AddGeneralInformationResult %@",soapResults);
    if([soapResults isEqualToString:@"true"])
    {


  [objDB updateDatabaseTable:@"GeneralInformation":@"Yes":
    [[arrGenralInfo          objectAtIndex:0] intValue]];
    }
    [soapResults setString:@""];
    elementFound = FALSE;
   }


  }
 -(void) connection:(NSURLConnection *) connection
 didReceiveResponse:(NSURLResponse *) response {
 [webData setLength: 0];

//NSLog(@"receive response");
  }  

 -(void) connection:(NSURLConnection *) connection
 didReceiveData:(NSData *) data {
  [webData appendData:data];
  }

    -(void) connection:(NSURLConnection *) connection
       didFailWithError:(NSError *) error {

    //NSLog(@"Server error");

    }

  -(void) connectionDidFinishLoading:(NSURLConnection *) connection {
     //NSLog(@"DONE. Received Bytes: %d", [webData length]);
      NSString *theXML = [[NSString alloc]
                    initWithBytes: [webData mutableBytes]
                    length:[webData length]
                    encoding:NSUTF8StringEncoding];
//---shows the XML---
//NSLog(@"%@",theXML);

// [activityIndicator stopAnimating];
if (xmlParser)
{

}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];

}

Popeye
  • 11,839
  • 9
  • 58
  • 91
Dinesh Patel
  • 142
  • 1
  • 7
2

Yes you can send header from iPhone as well as Android phone

Sajan Sharma
  • 109
  • 5
1

This is how you can append tokens in 'Post' request. Sample code :

NSURL *url = [NSURL URLWithString:@"http://www.deveinvoice.sirus/process/user.asmx"];
req = [NSMutableURLRequest requestWithURL:url];

//---set the headers---

NSString *msgLength = [NSString stringWithFormat:@"%d",[strSoapMsg length]];
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/CheckTransactionIDExist" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---

[req setHTTPMethod:@"POST"];
[req setHTTPBody: [strSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];
Rushi
  • 4,553
  • 4
  • 33
  • 46
  • Hi @Rushi thanks for your response. I just want to know that iPhone and android are capable to send header along with GET/POST request. – SP Singh Feb 21 '13 at 05:36
  • Yes. You can send header along with GET/POST method in iPhone and Android. – Rushi Feb 21 '13 at 05:39
1

Request for parsing xml data.

Suppose that is the method for request-

-(void)PostPipeSeAamAnomaly {

NSString *soapMsg =
[NSString stringWithFormat:
 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 "<soap:Envelope xmlns:xsi="
 "\"http://www.w3.org/2001/XMLSchema-instance\" "
 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
 "<soap:Body>"
 "<PostPipeSeAamAnomaly xmlns=\"http://tempuri.org/\">"
 "<PDAIRID>%d</PDAIRID>n"
 "<AssociatedItemIDILI>%@</AssociatedItemIDILI>n"
 "<AssociatedItemIDOther>%@</AssociatedItemIDOther>n"
 "<RGW>%@</RGW>n"
 "<StartDistance>%@</StartDistance>n"
 "<Length>%@</Length>n"
 "<Width>%@</Width>n"
 "<Orientation1>%@</Orientation1>n"
 "<Orientation2>%@</Orientation2>n"
 "<AnomalyType>%@</AnomalyType>n"
 "<OrientationToWeld>%@</OrientationToWeld>n"
 "<LongestLinearIndication>%@</LongestLinearIndication>n"
 "<LocalWallThickness>%@</LocalWallThickness>n"
 "<AnomalyDepthMinimum>%@</AnomalyDepthMinimum>n"
 "<AnomalyDepthAverage>%@</AnomalyDepthAverage>n"
 "<AnomalyDepthMaximum>%@</AnomalyDepthMaximum>n"
 "<AnomalyDepthSurface>%@</AnomalyDepthSurface>n"
 "<AnomalyDepthMaximumPercentage>AA</AnomalyDepthMaximumPercentage>n"
 "<AnomalyDepthMinimumPercentage>AA</AnomalyDepthMinimumPercentage>n"
 "<SizingMethod>%@</SizingMethod>n"
 "<AnomalyRemoved>%@</AnomalyRemoved>n"
 "<RemedialActionRequired>%@</RemedialActionRequired>n"
 "<RemedialActionIncidental>%@</RemedialActionIncidental>n"
 "<Comment>%@</Comment>n"
 "</PostPipeSeAamAnomaly>n"
 "</soap:Body>"
 "</soap:Envelope>",PDAirID ,[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:3],[arrPipeSeamAnomaly objectAtIndex:4] ,[arrPipeSeamAnomaly objectAtIndex:5] ,[arrPipeSeamAnomaly objectAtIndex:6],[arrPipeSeamAnomaly objectAtIndex:7] ,[arrPipeSeamAnomaly objectAtIndex:8] ,[arrPipeSeamAnomaly objectAtIndex:9],[arrPipeSeamAnomaly objectAtIndex:10],[arrPipeSeamAnomaly objectAtIndex:11] ,[arrPipeSeamAnomaly objectAtIndex:12] ,[arrPipeSeamAnomaly objectAtIndex:13],[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:2],[arrPipeSeamAnomaly objectAtIndex:14] ,[arrPipeSeamAnomaly objectAtIndex:15],[arrPipeSeamAnomaly objectAtIndex:16] ,[arrPipeSeamAnomaly objectAtIndex:17],[arrPipeSeamAnomaly objectAtIndex:18],[arrPipeSeamAnomaly objectAtIndex:19] ,[arrPipeSeamAnomaly objectAtIndex:20]];

//NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:
              WEBSERVICEURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/PostPipeSeAamAnomaly" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

//[activityIndicator startAnimating];


conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [NSMutableData data];


    //NSLog(@"go to delegateMethods");
}

}

Dinesh Patel
  • 142
  • 1
  • 7