<p>newbie programmer here :) Please go easy :)</p>
<p>net.http package documentation says
"For control over HTTP client headers, redirect policy, and other settings, create a Client"</p>
<p>When i am doing a get or a post, when do I want to create a client? I don't get when I'd want control over my headers. </p>
<p>I see this kind of thing in scripts, and don't really understand what it is for
req.Header.Add("Content-Type", "application/json; charset=utf-8")</p>
<pre><code>client := &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't even know what that is, you don't have to bother with it.</p>
<p>If later on you learn what JSON is and you want a response in JSON, you'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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传