0

我有一个 Roda/Rack 应用程序。在后处理程序中,这是我通过表单接收的enctype="multipart/form-data" 数据图像——二进制数据——也可以通过它发送,这就是它的原因multipart/form-data

我的代码:

  r.body.rewind
  body_params = r.body.read

它返回这个:

  body_params

  => 
  "------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"utf8\"\r\n\r\n\xE2\x9C\x93\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"authenticity_token\"\r\n\r\n7/Q00bPqz5dKz7ktrlFMpK5mRMO/T8vwyDu1opc/3sdizUvEZrg6ECFSGUnLD4QcX3PHgj68l6k3Z9HTQE9/Jg==\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[image_temp_url]\"\r\n\r\n\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[counterfoil_name]\"\r\n\r\nName:\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[counterfoil_email]\"\r\n\r\nEmail:\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[event_name]\"\r\n\r\ndasf\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[event_date]\"\r\n\r\nfda\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[description]\"\r\n\r\n              \r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[text_color]\"\r\n\r\n#000\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[border_color]\"\r\n\r\n#cf2a27\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[paper_size]\"\r\n\r\nA4\r\n------WebKitFormBoundary123ABC\r\nContent-Disposition: form-data; 
    name=\"model123[font]\"\r\n\r\nArial\r\n------WebKitFormBoundary123ABC--\r\n"

现在,我如何解析参数以便我可以按名称检索它们,例如body_params["model123"]["description"]

4

1 回答 1

0

简单:只需使用request.params(或r.params):

2.5.0 :001 > body_params = r.body.tap(&:rewind).read
 => "-----------------------------17731730531088932352830400330\r\nContent-Disposition: form-data;
     name=\"model123[description]\"\r\n\r\nThis is the description.\r\n-----------------------------17731730531088932352830400330\r\nContent-Disposition: form-data;
     name=\"model123[font]\"\r\n\r\nComic Sans\r\n-----------------------------17731730531088932352830400330--\r\n"
2.5.0 :002 > r.params
 => {"model123"=>{"description"=>"This is the description.", "font"=>"This is something else."}}
2.5.0 :003 > request.params["model123"]["description"]
 => "This is the description."

和方法实际上#params来自. 这是可能的,因为 Roda 的请求对象,继承自它#bodyRack::RequestRoda::RodaRequest

于 2018-03-24T13:03:59.050 回答