Please tell me - what is wrong in the code? Sends the result: "status code 200". I need "status code 201".

NSMutableURLRequest * signUpRequest = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @ " http://staging.api.kiteflightapp.com "] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 15.0];

NSString *api_key = [NSString stringWithFormat:@"register?api_key=&g4sksgk0kspscc4oogo8wow0w0ocossg000og0so"]; NSString *name_first = [NSString stringWithFormat:@"name_first=&john"]; NSString *name_last = [NSString stringWithFormat:@"name_last=&Logan"]; NSString *email_address = [NSString stringWithFormat:@"email_address= &test1ios@test.com"]; NSString *password = [NSString stringWithFormat:@"password=&010101"]; NSString *device_token = [NSString stringWithFormat:@"device_token= &123456"]; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *currentSession = [NSURLSession sessionWithConfiguration:sessionConfiguration]; [signUpRequest setHTTPMethod:@"POST"]; [signUpRequest setHTTPBody:[api_key dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; [signUpRequest setHTTPBody:[name_first dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; [signUpRequest setHTTPBody:[name_last dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; [signUpRequest setHTTPBody:[email_address dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; [signUpRequest setHTTPBody:[password dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; [signUpRequest setHTTPBody:[device_token dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]]; [signUpRequest setValue:@"http://staging.api.kiteflightapp.com" forHTTPHeaderField:@"Content-Type"]; if (currentSession != nil) { } NSURLSessionDataTask *dataTask2 = [currentSession dataTaskWithRequest:signUpRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"Response: %@ %@\n", response, error); if (error == nil) { NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"result json: %@", jsonArray); } }]; [dataTask2 resume]; 
  • The header type is strange, and '&' in all fields is suspicious - Max Mikheyenko
  • @MaxMikheyenko, and how best to write them? I just started working with it - I don’t enter everything. - Vladislav Hodeev 1:08 pm
  • see in the documentation what should be there and write it :) - Max Mikheyenko
  • @ Thanks for the navigation!))) - Vladislav Hodeev
  • what do you expect? I can't start it myself, what service you are using I don't know. the maximum that I can tell you is what I already wrote - the content-type, this is a pointer to what is being transmitted, for example, 'video' or 'json', but not what you have. And you have everywhere in the parameters the '&', which is used to separate the query, for example, login=123&password=456 , so your & in the middle of the line most likely spoil something and the server cannot figure out what you are cramming to it. Well, in general, all that can be said on your issue. - Max Mikheyenko

1 answer 1

What is exactly wrong here is to use setHTTPBody several times in a row. It seems that the API comes from you urlencoded request, it should be done something like this: https://stackoverflow.com/questions/31027668/sending-post-data-nsurlsession

About working with the Nuno component read here: https://developer.apple.com/reference/foundation/nsmutableurlrequest

In general, before working with the network, you need to understand the concept of the HTTP request and url-encioded POST in particular. You can start here: https://ru.wikipedia.org/wiki/POST_(HTTP)

  • thank. The question has already been decided. Through a separate class, where it broke into threads and made a separate request in each. Thank! - Vladislav Hodeev