我正试图用Sinatra和MessagePack构建一个API,但我要自杀了:)
所以我用卷发写了一个帖子:
curl -X POST -H "Content-Type: application/x-msgpack" --data-binary '\x82\xA4uuid\xBD8asd76a-a8s7a6d87-asd76as8d76\xABcampaign_id\xA12' http://localhost:9393/在辛纳特拉,我只想:
MessagePack.unpack request.body.read这将导致以下错误:
MessagePack::MalformedFormatError Exception: extra bytes follow after a deserialized object因为request.body.read正在返回:
"\\x82\\xA4uuid\\xBD8asd76a-a8s7a6d87-asd76as8d76\\xABcampaign_id\\xA12"
instead of
"\x82\xA4uuid\xBD8asd76a-a8s7a6d87-asd76as8d76\xABcampaign_id\xA12"我尝试了所有我能想到的东西,比如force_encoding(编码::二进制)和其他愚蠢的东西。我不知道是谁造成了这个问题--红宝石,利克还是辛纳特拉?
发布于 2014-04-28 15:24:16
问题在于您期望curl --data-binary从命令行做什么。它不使用类似Ruby的语法将'\x82'处理为字节值。它将字符设置为-is(如果您检查Ruby中的字符串,则类似于"\\x82" )。
用@filename语法代替curl,保存使用MessagePack生成的数据文件(确保在MessagePack中将模式设置为'wb' ):
curl -X POST -H "Content-Type: application/x-msgpack" --data-binary @test.dat http://localhost:9393/创建测试文件的Ruby:
msg = MessagePack.pack(
"uuid" => "8asd76a-a8s7a6d87-asd76as8d76",
"campaign_id" => "2"
)
File.open( 'test.dat', 'wb' ) do |file|
file.write( msg )
endhttps://stackoverflow.com/questions/23344173
复制相似问题