0

I am very new to iOS. I am trying to send data through post method to PHP. In PHP it can't take data like $_POST['data'], but it takes $_GET['data']. My iOS code is as follows.

NSString *strURL = [NSString stringWithFormat:@"http://example.com/app_respond_to_job?emp_id=%@&job_code=%@&status=Worker-Accepted&comment=%@",SaveID2,txtJobcode1,alertTextField.text];

NSURL *apiURL = [NSURL URLWithString:strURL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:apiURL];

[urlRequest setHTTPMethod:@"POST"];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

_receivedData = [[NSMutableData alloc] init];

[connection start];
NSLog(@"URL---%@",strURL);

Can someone explain why is that, it will be very helpful.

3 Answers 3

3

Please Download this file https://www.dropbox.com/s/tggf5rru7l3n53m/AFNetworking.zip?dl=0

And import file in your project Define in #import "AFHTTPRequestOperationManager.h"

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"Your Url"]];
   NSDictionary *parameters = @{@"emp_id":SaveID2,@"job_code":txtJobcode1.text,@"status":alertTextField.text};

   AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

   } success:^(AFHTTPRequestOperation *operation, id responseObject) {


      NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
      manager.responseSerializer = [AFHTTPResponseSerializer serializer];
      [responseObject valueForKey: @"data"];



   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

      NSLog(@"Error: %@ ***** %@", operation.responseString, error);
   }];
   [op start]; 
Sign up to request clarification or add additional context in comments.

Comments

0

because you send your data via query string in url
i think it will work if you try to pass data in your request's body:[urlRequest setHTTPBody:...]

Comments

0

POST parameters come from the request body, not from the URL string. You'll need to:

  • Call setHTTPBody on the request and provide the URL-encoded string (sans question mark, IIRC) as the body data
  • Call setValue:forHTTPHeaderField: to set the Content-Type to application/x-www-form-urlencoded
  • Either remove the call to [connection start] or use initWithRequest:delegate:startImmediately: so that you aren't starting the connection twice.

That last one is kind of important. You can get strange results if you try to start a connection twice. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.