Ruby拆分和解析批处理HTTP响应(多部分/混合)

Ruby拆分和解析批处理HTTP响应(多部分/混合),ruby,http,httpresponse,gmail-api,Ruby,Http,Httpresponse,Gmail Api,我正在使用GMail API,它允许我获得多个GMail对象的批处理响应。 这以多部分/混合HTTP响应的形式返回,其中包含一组单独的HTTP响应,这些响应由标头中定义的边界分隔。 每个HTTP子响应都是JSON格式 i、 e result.response.response_头={。。。 “内容类型”=>“多部分/混合;边界=批次”\u abcdefg“。。。 } result.response.body=“----batch\u abcdefg {some JSON} --批次号abcdef

我正在使用GMail API,它允许我获得多个GMail对象的批处理响应。 这以多部分/混合HTTP响应的形式返回,其中包含一组单独的HTTP响应,这些响应由标头中定义的边界分隔。 每个HTTP子响应都是JSON格式

i、 e

result.response.response_头={。。。
“内容类型”=>“多部分/混合;边界=批次”\u abcdefg“。。。
}
result.response.body=“----batch\u abcdefg
{some JSON}
--批次号abcdefg
{some JSON}
--批次_abcdefg--“

有没有库或简单的方法将这些响应从字符串转换为一组单独的HTTP响应或JSON对象?

多亏了上面的Thole

def parse_batch_response(response, json=true)
  # Not the same delimiter in the response as we specify ourselves in the request,
  # so we have to extract it. 
  # This should give us exactly what we need.
  delimiter = response.split("\r\n")[0].strip
  parts = response.split(delimiter)
  # The first part will always be an empty string. Just remove it.
  parts.shift
  # The last part will be the "--". Just remove it.
  parts.pop

  if json  
    # collects the response body as json
    results = parts.map{ |part| JSON.parse(part.match(/{.+}/m).to_s)}
  else
    # collates the separate responses as strings so you can do something with them
    # e.g. you need the response codes
    results = parts.map{ |part| part}
  end
  result
end
我刚才回答了。也许你能从那里得到一些灵感!
def parse_batch_response(response, json=true)
  # Not the same delimiter in the response as we specify ourselves in the request,
  # so we have to extract it. 
  # This should give us exactly what we need.
  delimiter = response.split("\r\n")[0].strip
  parts = response.split(delimiter)
  # The first part will always be an empty string. Just remove it.
  parts.shift
  # The last part will be the "--". Just remove it.
  parts.pop

  if json  
    # collects the response body as json
    results = parts.map{ |part| JSON.parse(part.match(/{.+}/m).to_s)}
  else
    # collates the separate responses as strings so you can do something with them
    # e.g. you need the response codes
    results = parts.map{ |part| part}
  end
  result
end