goadesign / goa

🌟 Goa: Elevate Go API development! 🚀 Streamlined design, automatic code generation, and seamless HTTP/gRPC support. ✨
https://goa.design
MIT License
5.69k stars 560 forks source link

setting the accept header doesn't seem to work #3387

Closed tibers closed 1 year ago

tibers commented 1 year ago

I'm working through the example documentation and I expected accept to work.

➜  calcsvc curl -vvvvvv -X 'GET' \
  'http://localhost:8088/multiply/10/10' \
  -H 'accept: application/json'
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying 127.0.0.1:8088...
* Connected to localhost (127.0.0.1) port 8088 (#0)
> GET /multiply/10/10 HTTP/1.1
> Host: localhost:8088
> User-Agent: curl/8.1.2
> accept: application/json
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Tue, 10 Oct 2023 19:30:23 GMT
< Content-Length: 4
<
100
* Connection #0 to host localhost left intact

On the face of it, not bad. But backstage expects JSON if it sends the accept header and I expected the output to be something along the lines of { "Result": "10" }. (Or even the name of the route...)

Peeking at the -debug output I can even see it notices the accept: bit...

[calcapi] 15:35:06 id=p4OZH87_ req=GET /multiply/10/10 from=127.0.0.1
> [p4OZH87_] GET /multiply/10/10
> [p4OZH87_] Accept: application/json
> [p4OZH87_] User-Agent: curl/8.1.2
> [p4OZH87_] a: 10
> [p4OZH87_] b: 10
< [p4OZH87_] OK
< [p4OZH87_] Content-Type: application/json
[p4OZH87_] 100
[p4OZH87_]

Is there a graceful way of handling this that just isn't apparent to me?

yngveh commented 1 year ago

Try change the result in design.go from Result(Int) to

Result(func() {
    Attribute("result", Int)
    Required("result")
})

Regenerate (goa gen calcsvc/design)

Change calc.go to

func (s *calcsrvc) Multiply(ctx context.Context, p *calc.MultiplyPayload) (res *calc.MultiplyResult, err error) {
    return &calc.MultiplyResult{Result: p.A * p.B}, nil
}

Build and run, switching the accept header from "application/json" to "application/xml" will now work as expected.

tibers commented 1 year ago

oh thank you very much! I would humbly suggest updating the tutorial because I felt it was some problem in the Body. :)

raphael commented 1 year ago

@tibers Yeah we could change the tutorial :) Would you mind giving it a shot?

tibers commented 1 year ago

Gonna write some readme!

raphael commented 1 year ago

The update looks great, thank you!