Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #114. support query string in path #115

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log #

## v2.3.1 ##

* `[FIX]` [#114](https://github.com/huandu/facebook/pull/114) Query string in the path (e.g. `fb.Get("/me?fields=name,email", nil)`) works as expected now. Thanks, [@AsifArko](https://github.com/AsifArko).

## v2.3.0 ##

* `[FIX]` [#110](https://github.com/huandu/facebook/pull/110) Use HTTP GET to send request in which the method is `GET`. Thank [@nayakravi](https://github.com/nayakravi) for raising this issue, and Thank [@AlphaB](https://github.com/AlphaB) and [@robbiet480](https://github.com/robbiet480) for your valuable inputs.
Expand Down
29 changes: 29 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"
)

// Graph API debug mode values.
Expand Down Expand Up @@ -343,6 +344,34 @@ func (session *Session) graph(path string, method Method, params Params) (res Re

session.prepareParams(params)

// parse path only if path contains '?'.
// url.ParseRequestURI cannot parse uri without "/" like "me".
if strings.Contains(path, "?") {
// parse query string in path.
u, e := url.ParseRequestURI(path)

if e != nil {
err = e
return
}

path = u.Path

if u.RawQuery != "" {
query, e := url.ParseQuery(u.RawQuery)

if e != nil {
err = e
return
}

// use these queries to overwrite the value in params.
for k := range query {
params[k] = query.Get(k)
}
}
}

var urlParams Params

if method == GET {
Expand Down
33 changes: 33 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func TestUploadingBinary(t *testing.T) {
t.Skipf("skip this case as we don't have a valid access token.")
}

t.Skipf("facebook doesn't support uploading photo to timeline.")

buf := bytes.NewBufferString(FB_TEST_BINARY_JPG_FILE)
reader := base64.NewDecoder(base64.StdEncoding, buf)

Expand Down Expand Up @@ -367,3 +369,34 @@ func TestSessionWithCustomBaseUrl(t *testing.T) {
t.Fatal("no call to mock server")
}
}

func TestGetWithQueryString(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}

session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)

id, err := session.User()

if err != nil {
t.Fatalf("cannot get current user id. [e:%v]", err)
}

t.Logf("current user id is %v", id)

result, e := Api("/me?access_token="+FB_TEST_VALID_ACCESS_TOKEN, GET, Params{
"fields": "name,email",
})

if e != nil {
t.Fatalf("cannot get my extended info. [e:%v]", e)
}

if result.Get("name") == nil || result.Get("email") == nil {
t.Fatalf("fail to get my extend info. [result:%v]", result)
}

t.Logf("my extended info is: %v", result)
}