title: Golang: io.Reader w/io.NopCloser
body: <p>I cannot for the life of me figure out why this isn't working... it returns a <code>404</code>... however it works when I remove <code>io.NopCloser</code></p>
  <p><strong>200:</strong>  <code>req, err := http.NewRequest(&quot;POST&quot;, uri, body)</code></p>
  <p><strong>404:</strong> <code>req, err := http.NewRequest(&quot;POST&quot;, uri, io.NopCloser(body))</code></p>
  <p>Why does this matter? Because while the signature of <code>NewRequest</code> accepts <code>io.Reader</code> inside the method its converted to a <code>io.ReadCloser</code></p>
  <p><a href="https://github.com/golang/go/blob/e94f7df957b6cfbdfbed7092fd05628452c5e018/src/net/http/request.go#L873" rel="nofollow noreferrer">https://github.com/golang/go/blob/e94f7df957b6cfbdfbed7092fd05628452c5e018/src/net/http/request.go#L873</a></p>
  <pre class="lang-golang prettyprint-override"><code>    rc, ok := body.(io.ReadCloser)
      if !ok &amp;&amp; body != nil {
          rc = io.NopCloser(body)
      }
  </code></pre>
  <p>Additionally the property <code>Request.Body</code> is of type <code>io.ReadCloser</code> (not <code>io.Reader</code>)</p>
  <p><a href="https://github.com/golang/go/blob/e94f7df957b6cfbdfbed7092fd05628452c5e018/src/net/http/request.go#L182" rel="nofollow noreferrer">https://github.com/golang/go/blob/e94f7df957b6cfbdfbed7092fd05628452c5e018/src/net/http/request.go#L182</a></p>
  <p>Ultimately my goal is to acheive this... hence why I do not want to set body when calling <code>http.NewRequest</code></p>
  <pre class="lang-golang prettyprint-override"><code>req, err := http.NewRequest(&quot;POST&quot;, uri, nil)
  
  if someCondition {
      req.Body = getBody()//some method that returns a io.ReaderCloser
  }
  
  //the real getBody method is more complex than this...
  func getBody() io.ReaderCloser {
      data := map[string][]string{
          &quot;scope&quot;:         {&quot;https://graph.microsoft.com/.default&quot;},
          &quot;client_id&quot;:     {&quot;0000-0000-0000-0000&quot;},
          &quot;client_secret&quot;: {&quot;REDACTED_CLIENT_SECRET&quot;},
          &quot;grant_type&quot;:    {&quot;client_credentials&quot;},
      }
  
      return io.NopCloser(strings.NewReader(url.Values(data).Encode()))
  }
  </code></pre>
  <p>Complete code that returns a <code>404</code> below...</p>
  <pre class="lang-golang prettyprint-override"><code>    uri := &quot;https://login.microsoftonline.com/REDACTED.onmicrosoft.com/oauth2/v2.0/token&quot;
  
      data := map[string][]string{
          &quot;scope&quot;:         {&quot;https://graph.microsoft.com/.default&quot;},
          &quot;client_id&quot;:     {&quot;0000-0000-0000-0000&quot;},
          &quot;client_secret&quot;: {&quot;REDACTED_CLIENT_SECRET&quot;},
          &quot;grant_type&quot;:    {&quot;client_credentials&quot;},
      }
  
      body := strings.NewReader(url.Values(data).Encode())
  
      req, err := http.NewRequest(&quot;POST&quot;, uri, io.NopCloser(body))
      if err != nil {
          panic(err)
      }
  
      client := &amp;http.Client{Timeout: 10 * time.Second}
      res, err := client.Do(req)
      if err != nil {
          panic(err)
      }
  
      b, err := io.ReadAll(res.Body)
      if err != nil {
          panic(err)
      }
  
      fmt.Println(res.StatusCode)
      fmt.Println(string(b))
  </code></pre>