rspec_api_documentation icon indicating copy to clipboard operation
rspec_api_documentation copied to clipboard

Cannot easily POST to URL with query string and body params

Open timcheadle opened this issue 8 years ago • 2 comments

I frequently deal with APIs that require POST requests to a URL that contains a query string. The problem is that I also need to send a body payload along with this request.

As you can see here, the code currently doesn't make this easy: https://github.com/zipmark/rspec_api_documentation/blob/master/lib/rspec_api_documentation/dsl/endpoint.rb#L39

Right now I'm getting around it with this hack:

header "Content-Type", "application/json"

post "/api/endpoint" do
  before do
    # Send auth token in URL
    example.metadata[:route] += "?token=#{auth_token}"
  end

  example "send json" do
    do_request(param: "value", other_param: "other value")
  end
end

It seems like this might be made easier if do_request accepted named arguments indicating which set of params go in the query string versus the request body. Introspecting based on the HTTP verb is pretty limiting.

timcheadle avatar May 23 '17 22:05 timcheadle

I too am running into this, just on DELETE requests instead and with some recent changes in rack-test, I'm hitting this a lot. I'd suggest following/reading on https://github.com/rack-test/rack-test/pull/223 Additionally, stickying rack-test to ~> 0.7.0 works for me, or you set your query string like above suggests.

I've been using the #parameter aspect of the DSL to better document the body of the request, and this causes issues when some should go in the query string, and some go in the body. If there is a better way to do this, then I'd love to hear it. Otherwise, I feel like there should be some way to denote these differences, along with allowing do_request to better accept query string vs body parameters.

JoshAshby avatar Mar 12 '18 01:03 JoshAshby

here's what I've done, something like

  # NOTE: This is the only way to get query params in a POST
  # https://github.com/zipmark/rspec_api_documentation/blob/6.1.0/lib/rspec_api_documentation/dsl/endpoint.rb#L44-L48
  all_the_includes = [
    "my_things", "your_things",
  ].map {|element| element.tr("_", "-") }
  post "/v1/thing-duplications?meta[thing-duplications]=copied_relations&include=#{all_the_includes.join(",")}", :authenticated do
    include_context "create parameters"

    example "POST create" do

bf4 avatar Jul 08 '20 03:07 bf4