<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: fuchiao</title>
    <description>The latest articles on DEV Community by fuchiao (@fuchiao).</description>
    <link>https://dev.to/fuchiao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255907%2F0dc67d27-517a-446c-9ae7-4254235611ca.jpg</url>
      <title>DEV Community: fuchiao</title>
      <link>https://dev.to/fuchiao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fuchiao"/>
    <language>en</language>
    <item>
      <title>set cookies for cross origin requests</title>
      <dc:creator>fuchiao</dc:creator>
      <pubDate>Sat, 02 Nov 2019 02:06:35 +0000</pubDate>
      <link>https://dev.to/fuchiao/set-cookies-for-cross-origin-requests-5b39</link>
      <guid>https://dev.to/fuchiao/set-cookies-for-cross-origin-requests-5b39</guid>
      <description>&lt;p&gt;When &lt;code&gt;http://portal&lt;/code&gt; sent a request to &lt;code&gt;http://auth&lt;/code&gt;, &lt;code&gt;http://auth&lt;/code&gt; failed to set user's cookie.&lt;/p&gt;

&lt;p&gt;Set credential flag for request and modified the following response headers to fix it &lt;a href="https://stackoverflow.com/questions/46288437/set-cookies-for-cross-origin-requests"&gt;[1]&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin"&gt;[2]&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access-Control-Allow-Origin (request.Header.Origin, no wildcard allowed)&lt;/li&gt;
&lt;li&gt;Vary (Origin)&lt;/li&gt;
&lt;li&gt;Access-Control-Allow-Credentials (true)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And we can followe &lt;a href="https://www.w3schools.com/xml/xml_http.asp"&gt;w3schoool article&lt;/a&gt; to test cross domain request by browser console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;xhttp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;xhttp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onreadystatechange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;xhttp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://auth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;xhttp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>webdev</category>
    </item>
    <item>
      <title>http redirect for golang http client</title>
      <dc:creator>fuchiao</dc:creator>
      <pubDate>Sat, 26 Oct 2019 16:14:53 +0000</pubDate>
      <link>https://dev.to/fuchiao/http-redirect-for-golang-http-client-2i35</link>
      <guid>https://dev.to/fuchiao/http-redirect-for-golang-http-client-2i35</guid>
      <description>&lt;p&gt;I had a problem today. &lt;/p&gt;

&lt;p&gt;I wrote a program to send an http request to my server, and the debugger told me that the http server returned 302. However, my client program always got 400.&lt;/p&gt;

&lt;h3&gt;
  
  
  today i learned
&lt;/h3&gt;

&lt;p&gt;Finally, I found that golang http client ignoreed 301 and 302. The client redirected to the new location and returned the new response only.&lt;/p&gt;

&lt;p&gt;Thanks, wireshark.&lt;/p&gt;

&lt;p&gt;And we can prevent redirects by the following configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CheckRedirect&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;via&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ErrUseLastResponse&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://www.jonathanmh.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  reference
&lt;/h3&gt;

&lt;p&gt;[1] &lt;a href="https://jonathanmh.com/tracing-preventing-http-redirects-golang/"&gt;https://jonathanmh.com/tracing-preventing-http-redirects-golang/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
