重定向 Sinatra 请求更改方法和正文 [英] Redirect Sinatra request changing method and body

查看:45
本文介绍了重定向 Sinatra 请求更改方法和正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Sinatra 上处理 GET 请求并在同一服务器上使用不同的主体发出 PATCH 请求?用户发出请求 GET/clean_beautiful_api 并且服务器将其重定向到 PATCH/dirty/clogged_api_url_1?crap=2 "{request_body: 1}"?

Is there a way to handle a GET request on Sinatra and make a PATCH request with a different body on the same server? User makes a request GET /clean_beautiful_api and server redirects it to PATCH /dirty/clogged_api_url_1?crap=2 "{request_body: 1}"?

我想在不干扰功能的情况下清理遗留 API.

I want to clean up legacy API without interfering with the functionality.

推荐答案

如果我没理解错的话,最简单的方法是将用于 patch 的块提取到一个帮助程序中:

If I've understood correctly, the easiest way is to extract the block used for the patch into a helper:

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

到:

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

或者你可以使用 lambda ......

Or you could use a 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.

您还可以通过以下方式使用 Rack 接口在内部触发另一条路由调用.

You can also trigger another route internally using the Rack interface via call.

这篇关于重定向 Sinatra 请求更改方法和正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆