title: HTTP NTLM authentication
body: <p>I am trying to consume an API which requires NTLM authentication.
  This <code>curl</code> command works fine:</p>
  
  <pre><code>curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json'  --ntlm -u user:password -d '{ "key1": "100",     "key2": "1"}' http://some/api/v/12
  </code></pre>
  
  <p>Now I am trying to do the same from a Go program:</p>
  
  <pre><code>package main
  
  import (
      "bytes"
      "fmt"
      "net/url"
      "net/http"
      "io/ioutil"
      "log"
      "github.com/Azure/go-ntlmssp"
  
  
  
  )
  
  func main() {
      url_ := "http://some/api/v/12"
  
       client := &amp;http.Client{
          Transport: ntlmssp.Negotiator{
              RoundTripper:&amp;http.Transport{},
          },
      }
  
  
  
      data := url.Values{}
      data.Set("key1", "100")
      data.Set("key2", "1")
      b := bytes.NewBufferString(data.Encode())
      req, err := http.NewRequest("POST", url_,  b)
      req.Header.Set("Content-Type", "application/json")
      req.Header.Set("Accept", "application/json")
      req.SetBasicAuth("user", "password")
  
  
      resp, err := client.Do(req)
      if err != nil {
          fmt.Printf("Error : %s", err)
      } else {
  
          responseData, err := ioutil.ReadAll(resp.Body)
          if err != nil {
              log.Fatal(err)
          }
  
  
          responseString := string(responseData)
  
  
  
          fmt.Println(responseString)
          resp.Body.Close()
      }
  
  }
  
  </code></pre>
  
  <p>When I execute this program I receive an "invalid credentials" error which I normally receive when I don't include "--ntlm" flag in the <code>curl</code> command.</p>
  
  <p>Can you please me give me a hint how can I accomplish this task with Go?</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>printing the request from the curl command:</p>
  
  <pre><code>* About to connect() to www.xxx.xxx.com port xx (#0)
  *   Trying xxx.xxx.x.xxx...
  * Connected to www.xxx.xxx.com (xxx.xxx.x.xx) port xx (#0)
  * Initializing NSS with certpath: sql:/etc/pki/nssdb
  * Server auth using NTLM with user 'user'
  
  
  &gt; POST /some/api/v2 HTTP/1.1
  &gt; Authorization: NTLM xxxxx (44 cahracters)
  &gt; User-Agent: curl/7.29.0
  &gt; Host: www.xxx.xxx.com
  &gt; Content-Type: application/json
  &gt; Accept: application/json
  &gt; Content-Length: 0
  &gt;
  
  &lt; HTTP/1.1 401 Unauthorized
  &lt; Content-Type: text/html; charset=us-ascii
  &lt; Server: Microsoft-HTTPAPI/2.0
  &lt; WWW-Authenticate: NTLM xxxxx (312 characters) 
  &lt; Date: Thu, xx Aug xxxx xx:xx:xx xxx
  &lt; Content-Length: 341
  &lt;
  
  * Ignoring the response-body
  * Connection
  * Issue another request to this URL: 'http://some/api/v2'
  * Found bundle for host www.xxx.xxx.com: 0x0000
  * Re-using existing connection! 
  * Connected to www.xxx.xxx.com (xxx.xxx.x.xx) port xx (#0)
  * Server auth using NTLM with user 'user'
  &gt; POST /api/v2 HTTP/1.1
  &gt; Authorization: NTLM xxx (176 characters)
  &gt; User-Agent: curl/7.29.0
  &gt; Host: www.xxx.xxx.com
  &gt; Content-Type: application/json
  &gt; Accept: application/json
  &gt; Content-Length: 39
  &gt;
  
  
  * upload completely sent off: 39 out of 39 bytes
  
  
  &lt; HTTP/1.1 200 OK
  &lt; Cache-Control: no-cache
  &lt; Pragma: no-cache
  &lt; Content-Type: application/json; charset=utf-8
  &lt; Expires: -1
  &lt; Server: Microsoft-IIS/7.5
  &lt; X-AspNet-Version: 4.0.30319
  &lt; Persistent-Auth: true
  &lt; X-Powered-By: ASP.NET
  &lt; Date: Thu, 08 Aug 2019 06:49:41 GMT
  &lt; Content-Length: 1235
  </code></pre>