When to create a http client? What is it good for?

xuanbao · · 469 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>newbie programmer here :) Please go easy :)</p> <p>net.http package documentation says &#34;For control over HTTP client headers, redirect policy, and other settings, create a Client&#34;</p> <p>When i am doing a get or a post, when do I want to create a client? I don&#39;t get when I&#39;d want control over my headers. </p> <p>I see this kind of thing in scripts, and don&#39;t really understand what it is for req.Header.Add(&#34;Content-Type&#34;, &#34;application/json; charset=utf-8&#34;)</p> <pre><code>client := &amp;http.Client{} resp, err := client.Do(req) </code></pre> <hr/>**评论:**<br/><br/>pobody: <pre><p>Not to put too fine a point on it, but you would control your headers when you hit a problem that requires you to control your headers.</p> <p>In the example you gave you are making a request for the response to be in JSON. If you don&#39;t even know what that is, you don&#39;t have to bother with it.</p> <p>If later on you learn what JSON is and you want a response in JSON, you&#39;ll find you have to set the header, and there you are.</p></pre>rondr: <pre><p>Ah, so that is one good example...I need to set a header to get a response in JSON. And if I don&#39;t set that in the header, presumably I can get my response in some other format and then marshal it should I want...?</p></pre>ChristophBerger: <pre><p>Looks like your question is about HTTP headers rather than about Go - correct me if I am wrong. </p> <p>HTTP Headers do not only control the format of the response but also <a href="https://en.m.wikipedia.org/wiki/List_of_HTTP_header_fields" rel="nofollow">a lot of other details</a>. Maybe you want to go through some HTTP tutorial (search the Web for <code>http header tutorial</code>) to learn about the purposes of the various HTTP headers.</p></pre>tex0: <pre><p>With a custom HTTP Client you can control client timeouts and TLS settings amongst others.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

469 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传