0

I have looked at a lot of posts about http post requests and I still have not been able to find the answer. My problem is that I need to send one string to my work partners website so it can be accessed using a GET request and used for a leader board/

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[req setHTTPMethod:@"POST"];
NSData *postData = [revs  dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[req addValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                            // Do something with response data here - convert to JSON, check if error exists, etc....
                                            NSLog(response.description);
                                           // NSLog([[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
                                        }];

[task resume];

But the request never goes to the website, it is called ../distances.txt

Can anyone help?

Kazunori Takaishi
  • 2,268
  • 1
  • 15
  • 27
Sasha
  • 1
  • I am getting a response.description back but, the postData is not showing up on the websites somehting.txt file. – Sasha Jun 03 '18 at 23:00
  • Is the error non-nil? ASCII isn't the default encoding for a POST. See https://stackoverflow.com/questions/708915/detecting-the-character-encoding-of-an-http-post-request#708942. You can also check the HTTP response code as referenced in https://stackoverflow.com/questions/25431042/nsurlresponse-how-to-get-status-code – particleman Jun 03 '18 at 23:10
  • Thank you for the response, the error is nil – Sasha Jun 03 '18 at 23:29
  • I set the content type with this statement [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; – Sasha Jun 03 '18 at 23:33
  • I am getting a 412 status response for some reason. – Sasha Jun 03 '18 at 23:36
  • The 412 status is "The server does not meet one of the preconditions that the requester put on the request." https://httpstatuses.com/412. So the server doesn't meet the criteria specified in your POST headers. Try a content type of text/plain – particleman Jun 04 '18 at 00:24
  • You can change your encoding to NSUTF8StringEncoding (text/plain ;charset="UTF-8"). See https://stackoverflow.com/questions/17934505/is-there-any-disadvantage-of-using-text-plain-charset-utf-8 – particleman Jun 04 '18 at 00:27
  • I changed the encodings and no luck, It was weird I was able to get a 200 response one time, and then it was back to 412. I think the problem may be with the website im posting too? – Sasha Jun 04 '18 at 01:21
  • Without knowing the details of the endpoint to which you're posting, it would seem that it's not configured to accept the data in the way you're sending it. You could try using a `NSURLSessionUploadTask` if you think the target server can accept an uploaded file. – particleman Jun 04 '18 at 01:28
  • Thanks for the help particleman, We ended up switching gears to posting to a php file instead of a txt document and its working better. Stay away from triangleman! – Sasha Jun 04 '18 at 03:36
  • Glad you got it working. Triangle always wins. :( – particleman Jun 04 '18 at 14:05

0 Answers0