简体   繁体   English

重定向Sinatra请求更改方法和主体

[英]Redirect Sinatra request changing method and body

Is there a way to handle a GET request on Sinatra and make a PATCH request with a different body on the same server? 有没有一种方法可以处理Sinatra上的GET请求,并在同一服务器上使用其他主体发出PATCH请求? User makes a request GET /clean_beautiful_api and server redirects it to PATCH /dirty/clogged_api_url_1?crap=2 "{request_body: 1}" ? 用户发出请求GET /clean_beautiful_api ,服务器将其重定向到PATCH /dirty/clogged_api_url_1?crap=2 "{request_body: 1}"

I want to clean up legacy API without interfering with the functionality. 我想在不干扰功能的情况下清理旧版API。

If I've understood correctly, the easiest way is to extract the block used for the patch into a helper: 如果我正确理解,最简单的方法是将用于patch的块提取到帮助器中:

patch "/dirty/clogged_api_url_1"
  crap= params[:crap]
end

to: 至:

helpers do
  def patch_instead( params={} )
    # whatever you want to do in here
    crap= params[:crap]
  end
end

get "/clean_beautiful_api" do
  patch_instead( params.merge(request_body: 1) )
end


patch "/dirty/clogged_api_url_1"
  patch_instead( params )
end

Or you could use a lambda… 或者您可以使用lambda…

Patch_instead = ->( params={} ) {
  # whatever you want to do in here
  crap= params[:crap]
}

get "/clean_beautiful_api" do
  Patch_instead.call( params.merge(request_body: 1) )
end

# you get the picture

the main thing is to extract the method to somewhere else and then call it. 最主要的是将方法提取到其他位置,然后调用它。


Edit: You can also trigger another route internally using the Rack interface via call . 编辑:您还可以触发另一路在内部使用机架接口通过call

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM