Note that there are some explanatory texts on larger screens.

plurals
  1. POPOST request in iOS
    primarykey
    data
    text
    <p>In my app I'm trying to do a POST request to a server to add a product to a cart. My code is the follow:</p> <pre><code>#import "CreateCarriage.h" #import "NSData+Base64.h" @implementation CreateCarriage{ NSMutableData *datas; NSString *prodID; } - (void)createCarriageWithProductID:(NSString *)productID { self.qty = @"1"; prodID = productID; [self sendRequestToURL:self.buyUrl withMethod:@"POST"]; } - (id)sendRequestToURL:(NSString *)url withMethod:(NSString *)method { NSURL *finalUrl; if ([method isEqualToString:@"POST"]) { finalUrl = [NSURL URLWithString:url]; } else { NSLog(@"Metodo non previsto"); } NSString *post = [NSString stringWithFormat:@"form_key=%@&amp;product=%@&amp;related_product=&amp;qty=%@", self.formKey, prodID, self.qty]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl]; [request setHTTPMethod:method]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; [request setHTTPBody:postData]; NSLog(@"%@", request); NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; if (connection) { [connection start]; } return connection; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { datas = [[NSMutableData alloc]init]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]); [datas appendData:data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Oggetto aggiunto al carrello"); } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@", error); } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSString *username = @"user"; NSString *password = @"password"; NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } @end </code></pre> <p>When I run the app and try to add this product to the cart it shows this html:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Shopping Cart&lt;/title&gt; &lt;meta name="description" content="Default Description" /&gt; &lt;meta name="keywords" content="Magento, Varien, E-commerce" /&gt; &lt;meta name="robots" content="INDEX,FOLLOW" /&gt; &lt;link rel="icon" href="http://54.204.6.246/magento8/skin/frontend/default/default/favicon.ico" type="image/x-icon" /&gt; &lt;link rel="shortcut icon" href="http://54.204.6.246/magento8/skin/frontend/default/default/favicon.ico" type="image/x-icon" /&gt; &lt;!--[if lt IE 7]&gt; &lt;script type="text/javascript"&gt; //&lt;![CDATA[ var BLANK_URL = 'http://54.204.6.246/magento8/js/blank.html'; var BLANK_IMG = 'http://54.204.6.246/magento8/js/spacer.gif'; //]]&gt; &lt;/script&gt; &lt;![endif]--&gt; &lt;link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/default/default/css/styles.css" media="all" /&gt; &lt;link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/base/default/css/widgets.css" media="all" /&gt; &lt;link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/default/default/css/print.css" media="print" /&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/prototype/prototype.js"&gt;&lt;/script&gt; &lt;script type="tex 2013-11-14 10:59:52.735 BitmamaShop[1240:70b] t/javascript" src="http://54.204.6.246/magento8/js/lib/ccard.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/prototype/validation.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/builder.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/effects.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/dragdrop.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/controls.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/slider.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/js.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/form.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/menu.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/mage/translate.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/mage/cookies.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/weee.js"&gt;&lt;/script&gt; &lt;!--[if lt IE 8]&gt; &lt;link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/default/default/css/styles-ie.css" media="all" /&gt; &lt;![endif]--&gt; &lt;!--[if lt IE 7]&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/js/lib/ds-sleight.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://54.204.6.246/magento8/skin/frontend/base/default/js/ie6.js"&gt;&lt;/script&gt; &lt;![endif]--&gt; &lt;script type="text/javascript"&gt; //&lt;![CDATA[ Mage.Cookies.path = '/magento8'; Mage.Cookies.domain = '.54.204.6.246'; //]]&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; //&lt;![CDATA[ optionalZipCountries = ["HK","IE","MO","PA"]; //]]&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt;//&lt;![CDATA[ var Translator = new Translate([]); //]]&gt;&lt;/script&gt;&lt;/head&gt; &lt;body class=" checkout-cart-index"&gt; &lt;div class="wrapper"&gt; &lt;noscript&gt; &lt;div class="global-site-notice noscript"&gt; &lt;div class="notice-inner"&gt; &lt;p&gt; &lt;strong&gt;JavaScript seems to be disabled in your browser.&lt;/strong&gt;&lt;br /&gt; You must have JavaScript enabled in your browser to utilize the functionality of this website. &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/noscript&gt; &lt;div class="page"&gt; &lt;div class="header-container"&gt; &lt;div class="header"&gt; &lt;a href="http://54.204.6.246/magento8/" title="Magento Commerce" class="logo"&gt;&lt;strong&gt;Magento Commerce&lt;/strong&gt;&lt;img src="http://54.204.6.246/magento8/skin/frontend/default/default/images/logo.gif" alt="Magento Commerce" /&gt;&lt;/a&gt; &lt;div class="quick-access"&gt; &lt;form id="search_mini_form" action="http://54.204.6.246/magento8/catalogsearch/result/" method="get"&gt; &lt;div class="form-search"&gt; &lt;label for="search"&gt;Search:&lt;/label&gt; &lt;input id="search" type="text" name="q" value="" class="input-text" maxlength="128" /&gt; &lt;button type="submit" title="Search" class="button"&gt;&lt;span&gt;&lt;span&gt;Search&lt;/span&gt;&lt;/span&gt;&lt;/button&gt; &lt;div id="search_autocomplete" class="search-autocomplete"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; //&lt;![CDATA[ var searchForm = new Varien.searchForm('search_mini_form', 'search', 'Search entire store here...'); searchForm.initAutocomplete('http://54.204.6.246/magento8/catalogsearch/ajax/suggest/', 'search_autocomplete'); //]]&gt; &lt;/script&gt; &lt;/div&gt; &lt;/form&gt; &lt;p class="welcome-msg"&gt;Default welcome msg! &lt;/p&gt; &lt;ul class="links"&gt; &lt;li class="first" &gt;&lt;a href="http://54.204.6.246/magento8/customer/account/" title="My Account" &gt;My Account&lt;/a&gt;&lt;/li&gt; &lt;li &gt;&lt;a href="http://54.204.6.246/magento8/wishlist/" title="My Wishlist" &gt;My Wishlist&lt;/a&gt;&lt;/li&gt; &lt;li &gt;&lt;a href="http://54.204.6.246/magento8/checkout/cart/" title="My Cart" class="top-link-cart"&gt;My Cart&lt;/a&gt;&lt;/li&gt; &lt;li &gt;&lt;a href="http://54.204.6.246/magento8/checkout/" title="Checkout" class="top-link-checkout"&gt;Checkout&lt;/a&gt;&lt;/li&gt; &lt;li class=" last" &gt;&lt;a href="http://54.204.6.246/magento8/customer/account/login/" title="Log In" &gt;Log In&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="nav-container"&gt; &lt;ul id="nav"&gt; &lt;li class="level0 nav-1 first last level-top"&gt;&lt;a href="http://54.204.6.246/magento8/special-sales.html" class="level-top" &gt;&lt;span&gt;special_sales&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div class="main-container col1-layout"&gt; &lt;div class="main"&gt; &lt;div class="col-main"&gt; &lt;div class="page-title"&gt; &lt;h1&gt;Shopping Cart is Empty&lt;/h1&gt; &lt;/div&gt; &lt;div class="cart-empty"&gt; &lt;p&gt;You have no items in your shopping cart.&lt;/p&gt; &lt;p&gt;Click &lt;a href="http://54.204.6.246/magento8/api/rest/products/1"&gt;here&lt;/a&gt; to continue shopping.&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="footer-container"&gt; &lt;div class="footer"&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://54.204.6.246/magento8/about-magento-demo-store"&gt;About Us&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://54.204.6.246/magento8/customer-service"&gt;Customer Service&lt;/a&gt;&lt;/li&gt; &lt;li class="last privacy"&gt;&lt;a href="http://54.204.6.246/magento8/privacy-policy-cookie-restriction-mode"&gt;Privacy Policy&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;ul class="links"&gt; &lt;li class="first" &gt;&lt;a href="http://54.204.6.246/magento8/catalog/seo_sitemap/category/" title="Site Map" &gt;Site Map&lt;/a&gt;&lt;/li&gt; &lt;li &gt;&lt;a href="http://54.204.6.246/magento8/catalogsearch/term/popular/" title="Search Terms" &gt;Search Terms&lt;/a&gt;&lt;/li&gt; &lt;li &gt;&lt;a href="http://54.204.6.246/magento8/catalogsearch/advanced/" title="Advanced Search" &gt;Advanced Search&lt;/a&gt;&lt;/li&gt; &lt;li &gt;&lt;a href="http://54.204.6.246/magento8/sales/guest/form/" title="Orders and Returns" &gt;Orders and Returns&lt;/a&gt;&lt;/li&gt; &lt;li class=" last" &gt;&lt;a href="http://54.204.6.246/magento8/contacts/" title="Contact Us" &gt;Contact Us&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;p class="bugs"&gt;Help Us to Keep Magento Healthy - &lt;a href="http://www.magentocommerce.com/bug-tracking" onclick="this.target='_blank'"&gt;&lt;strong&gt;Report All Bugs&lt;/strong&gt;&lt;/a&gt; (ver. 1.8.0.0)&lt;/p&gt; &lt;address&gt;&amp;copy; 2013 Magento Demo Store. All Rights Reserved.&lt;/address&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>In which you can see that I didn't added anything to my cart... What's wrong with this code?</p> <p><strong>UPDATE:</strong></p> <p>I tried to use Chrome to see the POST/GET request that the browser did and I will post here the response:</p> <pre><code>Request URL:http://54.204.6.246/magento8/checkout/cart/add/uenc/aHR0cDovLzU0LjIwNC42LjI0Ni9tYWdlbnRvOC9zcGVjaWFsLXNhbGVzL2VhdS1kZS10b2lsZXQtMi5odG1sP19fX1NJRD1V/product/2/form_key/7CUKq8086ReKwIjs/ Request Method:POST Status Code:302 Moved Temporarily Request Headersview source Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip,deflate,sdch Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4 Authorization:Basic cmVwbHk6cmVwbHk= Cache-Control:max-age=0 Content-Length:58 Content-Type:application/x-www-form-urlencoded Cookie:PHPSESSID=hsbbokn9h4v0dnaa3oiis38jd5; frontend=8u4mii1suojhhmj27ndiqm4pf3 Host:54.204.6.246 Origin:http://54.204.6.246 Proxy-Connection:keep-alive Referer:http://54.204.6.246/magento8/special-sales/eau-de-toilet-2.html User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36 Form Dataview parsed form_key=7CUKq8086ReKwIjs&amp;product=2&amp;related_product=&amp;qty=1 Response Headersview source Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection:keep-alive Content-Length:0 Content-Type:text/html; charset=UTF-8 Date:Thu, 14 Nov 2013 09:35:13 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Location:http://54.204.6.246/magento8/checkout/cart/ Pragma:no-cache Server:Apache/2.2.15 (Red Hat) Set-Cookie:frontend=8u4mii1suojhhmj27ndiqm4pf3; expires=Thu, 14-Nov-2013 10:35:14 GMT; Max-Age=3600; path=/magento8; domain=54.204.6.246; httponly Via:1.1 ced01squidp02.replynet.prv (squid/3.3.9) X-Cache:MISS from ced01squidp02.replynet.prv X-Cache-Lookup:MISS from ced01squidp02.replynet.prv:8080 X-Powered-By:PHP/5.5.5 </code></pre> <p>I guess the section that I should use it's <code>Form Data</code> and it shows me this request: <code>form_key=7CUKq8086ReKwIjs&amp;product=2&amp;related_product=&amp;qty=1</code>, if you take a look about my iOS code you can see that I tried to do the same stuff.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload