title: Trying to make a HTTP request and return the result of that request in an app that uses go/gin
body: <p>I just picked up Go yesterday and I was wondering how I can make an HTTP request and return the result of that request with my API made with Gin.</p>
  <p>This is the code for the endpoint that returns the result of another request</p>
  <pre><code>func ProvideAccessToken(c *gin.Context) {
      body := bindings.RequestAccessTokenBody{}
  
      err := c.ShouldBind(&amp;body)
      if err != nil {
          c.JSON(http.StatusBadRequest, gin.H{&quot;error&quot;: err.Error()})
          log.Println(err)
          return
      }
  
      if body.Code == &quot;&quot; || body.GrantType == &quot;&quot; || body.RedirectURI == &quot;&quot; {
          c.String(http.StatusBadRequest, &quot;Bad Request&quot;)
          return
      }
      res, err := helpers.ExchangeCodeForToken(body.GrantType, body.RedirectURI, body.Code)
  
      if err != nil {
          c.JSON(res.StatusCode, gin.H{&quot;error&quot;: err.Error()})
          return
      }
      defer res.Body.Close()
      c.Header(&quot;Content-Type&quot;, &quot;application/json&quot;)
      c.JSON(200, res.Body)
  }
  </code></pre>
  <p>This is the function that makes the HTTP request</p>
  <pre><code>func ExchangeCodeForToken(grantType string, redirectUri string, code string) (*http.Response, error) {
      authToken := &quot;Basic &quot; + encodeClientSecretAndId()
      body := url.Values{}
      body.Set(&quot;grant_type&quot;, grantType)
      body.Set(&quot;redirect_uri&quot;, redirectUri)
      body.Set(&quot;code&quot;, code)
      encodedBody := body.Encode()
      client := &amp;http.Client{}
  
      r, _ := http.NewRequest(http.MethodPost, tokenUrl, strings.NewReader(encodedBody))
      r.Header.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
      r.Header.Add(&quot;Authorization&quot;, authToken)
      res, err := client.Do(r)
      return res, err
  }
  </code></pre>
  <p>When I use postman to make a request, the response is just an empty json like so<code>{}</code>.
  When change the end of my <code>ProvideAccessToken</code> function to be</p>
  <pre><code>    defer res.Body.Close()
      respbody, err := ioutil.ReadAll(res.Body)
      if err != nil {
          c.JSON(res.StatusCode, gin.H{&quot;error&quot;: err.Error()})
          return
      }
      c.Header(&quot;Content-Type&quot;, &quot;application/json&quot;)
      c.JSON(200, string(respbody))
  </code></pre>
  <p>I get this as my result <code>&quot;{\&quot;error\&quot;:\&quot;invalid_grant\&quot;,\&quot;error_description\&quot;:\&quot;Authorization code expired\&quot;}&quot;</code>, which is correct but it has the weird formatting. I just want to return exactly what the other API returns.</p>