{"id":2073,"date":"2017-03-06T21:13:46","date_gmt":"2017-03-07T02:13:46","guid":{"rendered":"https:\/\/bitpost.com\/news\/?p=2073"},"modified":"2017-03-06T21:21:13","modified_gmt":"2017-03-07T02:21:13","slug":"redirection-best-practice-http-302","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2017\/redirection-best-practice-http-302\/","title":{"rendered":"Redirection best practice: HTTP 302"},"content":{"rendered":"<p>When an unauthenticated user accesses a protected resource of my API server, I want to immediately redirect them to the login page. \u00a0Research has indicated that the server should send the client an HTTP 302 page with the url. \u00a0Experimentation has shown that you can successfully set cookies. The cookie can be used to hold the url to re-redirect the user back to the original request after authenticating. Nice.<\/p>\n<p>Here&#8217;s the header to send from the server:<\/p>\n<pre><code>\r\n    const string cstr_HTML_302_HEADER1 = \"HTTP\/1.1 302 Moved Temporarily\\r\\nLocation: \";\r\n    const string cstr_HTML_HEADER2 = \"\\r\\n\\r\\n\";\r\n    \/\/ ....\r\n        string cookie_header = \"\\r\\n\";\r\n        cookie_header += \"Set-Cookie: .... \";\r\n        *response << cstr_HTML_302_HEADER1 << \"\/v1\/login.html\" << cookie_header << cstr_HTML_HEADER2;\r\n<\/code><\/pre>\n<p>The publicly available version of quick-http on <a href=\"https:\/\/github.com\/moodboom\/quick-http\">github<\/a> needs a refresh, hopefully I'll have some time soon to move a large set of new code there that uses <a href=\"https:\/\/github.com\/eidheim\/Simple-Web-Server\">Simple Web Server<\/a>, HTTP 302 redirects, user authentication, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When an unauthenticated user accesses a protected resource of my API server, I want to immediately redirect them to the login page. \u00a0Research has indicated that the server should send the client an HTTP 302 page with the url. \u00a0Experimentation has shown that you can successfully set cookies. The cookie can be used to hold [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[233,10,9],"tags":[],"class_list":["post-2073","post","type-post","status-publish","format-standard","hentry","category-quick-http","category-tricks-tips-tools","category-websites"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-xr","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=2073"}],"version-history":[{"count":5,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2073\/revisions"}],"predecessor-version":[{"id":2078,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2073\/revisions\/2078"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=2073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=2073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=2073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}