Skip to content

Commit

Permalink
Properly pass nil body to http.NewRequest
Browse files Browse the repository at this point in the history
The integration tests revealed that AnimeService.List and
MangaService.List started receiving a 411 Length required error from the
MyAnimeList API. Both methods use mal.Client.NewRequest to create API
requests.

This is probably related to golang/go#18117
and it happened because when using http.NewRequest (inside
mal.Client.NewRequest) with GET, the body argument was not nil as it
should. This commit ensures that nil body is properly passed to
http.NewRequest when mal.Client.NewRequest has no data to encode.
  • Loading branch information
nstratos committed Mar 3, 2017
1 parent 7fd7ca8 commit e79bb93
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mal/mal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -159,16 +160,18 @@ func (c *Client) NewRequest(method, urlStr string, data interface{}) (*http.Requ

u := c.BaseURL.ResolveReference(rel)

v := url.Values{}
var body io.Reader
if data != nil {
d, merr := xml.Marshal(data)
if merr != nil {
return nil, merr
}
v := url.Values{}
v.Set("data", string(d))
body = strings.NewReader(v.Encode())
}

req, err := http.NewRequest(method, u.String(), strings.NewReader(v.Encode()))
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e79bb93

Please sign in to comment.