Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:12:19+00:00 2026-06-16T04:12:19+00:00

I’m trying to get a POST message working. I’ve seen a couple of posts

  • 0

I’m trying to get a POST message working. I’ve seen a couple of posts describing doing so, such as this one, but I still cannot get it working. Below is my objective-c code:

NSString * urlString = @"http://magicthegatheringdatabase.com/test.php";
NSURL *aUrl = [NSURL URLWithString: urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

NSString *postString = [NSString stringWithFormat:@"tes1=%@&test2=%@",
                        [@"hello" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        [@"world" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: postBody];

[request addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

NSString *postLength = [NSString stringWithFormat:@"%d", postBody.length];
[request addValue: postLength forHTTPHeaderField:@"Content-Length"];

[request setHTTPMethod:@"POST"];

NSError * error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request
                                           returningResponse: nil
                                                       error: &error];
NSLog(@"%p, %@", error, error.localizedDescription);

NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Result: %@", returnString);

And here is my PHP:

<?php
  print_r($_REQUEST);
  print_r($_POST);
  print_r($_GET);

  echo file_get_contents( 'php://input' );
?>

If I run the code, I get the following logged:

2012-12-19 09:24:09.061 MTG Deck Builder[7921:570f] 0x0, (null)
2012-12-19 09:24:09.062 MTG Deck Builder[7921:570f] Result: Array
(
)
Array
(
)
Array
(
)

If I navigate directly to the url and append ?test1=hello&test2=world (using GET obviously instead of POST) I get:

Array
(
    [test1] => hello
    [test2] => world
    [phpbb3_5e63r_u] => 1
    [phpbb3_5e63r_k] => 
    [phpbb3_5e63r_sid] => 7ff37e188aa8e0ab57fae6a52f0d1f7b
    [__utma] => 86369156.392372889.1349352986.1352901458.1353328580.11
    [__utmz] => 86369156.1351935106.8.2.utmcsr=hankinsoft.com|utmccn=(referral)|utmcmd=referral|utmcct=/website/forum/10-mtg-magic-the-gathering-trading-card-game-tcg-da/634-new-forum.html
    [style_cookie] => null
)
Array
(
)
Array
(
    [test1] => hello
    [test2] => world
)

So I know my PHP is logging the get/requests properly, but I’m not sure why my POST via the objective-c code is not working. I’m pretty sure that I’m overlooking something stupid. Any suggestions on what I am missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:12:20+00:00Added an answer on June 16, 2026 at 4:12 am

    This is because NSURLConnection doesn’t handle redirections properly.

    When you send the POST request to the URL http://magicthegatheringdatabase.com/test.php, the server respond with 301 Moved Permanently and redirects to http://www.magicthegatheringdatabase.com/test.php.

    Instead of sending the same request to the new URL, NSURLConnection creates a new GET request and discards the original request’s HTTPBody. THis behavior is due to the HTTP specifications that forbid the automatic redirection of requests other than HEAD and GET without a confirmation from the user. Changing the method to GET is erroneous but at least it is a safe method, which cannot harm the HTTP resource.

    This problem can be solved with two options:

    1. You may simply change the URL of the request to be http://www.magicthegatheringdatabase.com/test.php.

    2. Or you may create an instance of NSURLConnection with a delegate and implement the connection:willSendRequest:redirectResponse: in order to handle the redirection properly as described in the answer to this question. This will work even if the redirection changes but requires you to write more code as you need to wait for the response, create a NSMutableData object, append the bytes as they come and handle everything asynchronously (see Apple Developer Documentation). However, performing tasks asynchronously is always a good idea: you can cancel them if needed and you don’t risk blocking your thread for a long a time (when you’re in the main thread, anything longer than 1 second is a long time).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.