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

HTTP method is overridden #12

Closed
solher opened this issue Mar 3, 2016 · 6 comments
Closed

HTTP method is overridden #12

solher opened this issue Mar 3, 2016 · 6 comments

Comments

@solher
Copy link

solher commented Mar 3, 2016

I wanted to try gentleman doing a simple:

func (r *Repository) Send(authPayload, method, url string, data interface{}) ([]byte, error) {
    req := r.c.Request()
    req.AddHeader("Auth-Server-Payload", authPayload)
    req.Method(method)
    req.URL(url)
    req.JSON(data)
    res, err := req.Send()
    if err != nil {
        return nil, err
    }
        return res.Bytes(), nil
}

But when I use it, the HTTP method is overridden and the client always send a POST.

When I write fmt.Printf(req.Context.Request.Method) just before the Send and it prints a GET, I can see that a fmt.Printf(ctx.Request.Method) in the doDial method of the dispatcher prints a POST.

Am I doing something wrong ?

@h2non
Copy link
Owner

h2non commented Mar 3, 2016

Well, that scenario could happen when you define a client level HTTP method.

I've fixed the issue. Try updating gentleman.

go get -u gopkg.in/h2non/gentleman.v0

@h2non h2non closed this as completed Mar 3, 2016
@solher
Copy link
Author

solher commented Mar 3, 2016

Hum. It doesn't seems to solve the problem for me.

I don't define a client level HTTP method. I only do:

type Repository struct {
    c *gentleman.Client
}

func NewRepository() *Repository {
    return &Repository{c: gentleman.New()}
}

And then call the Send method.

@h2non
Copy link
Owner

h2non commented Mar 3, 2016

Have you tried it again updating gentleman?. It should work now since the method is define either in client or request level via middleware call chain.

@solher
Copy link
Author

solher commented Mar 4, 2016

Yeah the update is done.

// Method defines the HTTP verb to be used.
func (r *Request) Method(method string) *Request {
    r.Middleware.UseRequest(func(ctx *context.Context, h context.Handler) {
        ctx.Request.Method = method
        fmt.Print(ctx.Request.Method)
        h.Next(ctx)
    })
    return r
}

Prints GET while the same in the doDial method still prints POST.

@h2non
Copy link
Owner

h2non commented Mar 4, 2016

I see where is the issue. You're always calling JSON(), which implicitely defines the POST method if no method was previously defined. In that case you should consider calling that method only if payload data exists.

@solher
Copy link
Author

solher commented Mar 4, 2016

Oh ok ! Perfect, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants