如何将带有输出选项的curl命令转换为httparty?

时间:2012-05-06 10:06:42

标签: ruby curl httparty

我想转换这个:

curl -k -v -X GET -H "Accept: application/pdf" https://username:password@rest.click2mail.com/v1/mailingBuilders/456/proofs/1 -o myProof

表示httparty。这是我的代码:

@auth = {:username => 'test', :password => 'test'}
options = {:headers => {'Accept' => 'application/pdf'}, :basic_auth => @auth }
body = HTTMultiParty.get("https://stage.rest.click2mail.com/v1/mailingBuilders/54544/proofs/1", options)

File.open("myProof", "w") do |file|
  file.write body
end

p "Reponse #{body.parsed_response}"

响应返回

“无法将urn:c2m:document:id:361从text / plain转换为application / pdf”

修改(2)

带有“text / plain”的

body.inspect返回

#<HTTParty::Response:0x8 @parsed_response=nil, @response=#<Net::HTTPNotAcceptable 406 Not Acceptable readbody=true>, @headers={\"date\"=>[\"Sun, 06 May 2012 11:22:12 GMT\"], \"server\"=>[\"Jetty(6.1.x)\"], \"content-length\"=>[\"0\"], \"connection\"=>[\"close\"], \"content-type\"=>[\"text/plain; charset=UTF-8\"]}>

with“application / pdf”

#<HTTParty::Response:0x7fce08a92260 @parsed_response=\"Cannot convert urn:c2m:document:id:361 from text/plain to application/pdf\", @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={\"date\"=>[\"Sun, 06 May 2012 11:24:09 GMT\"], \"server\"=>[\"Jetty(6.1.x)\"], \"content-type\"=>[\"application/pdf\"], \"connection\"=>[\"close\"], \"transfer-encoding\"=>[\"chunked\"]}>

编辑3

Api:第8步

https://developers.click2mail.com/rest-api#send-a-test-mailing

编辑4

使用debug_ouput选项

with“application / pdf”

opening connection to stage.rest.click2mail.com...
opened
<- "GET /v1/mailingBuilders/54544/proofs/1 HTTP/1.1\r\nAccept: application/pdf\r\nAuthorization: Basic Ym9sb2RldjptVW43Mjk0eQ==\r\nConnection: close\r\nHost: stage.rest.click2mail.com\r\n\r\n"
-> "HTTP/1.1 400 Bad Request\r\n"
-> "Date: Sun, 06 May 2012 14:05:30 GMT\r\n"
-> "Server: Jetty(6.1.x)\r\n"
-> "Content-Type: application/pdf\r\n"
-> "Connection: close\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "\r\n"
-> "49\r\n"
reading 73 bytes...
-> ""
-> "Cannot convert urn:c2m:document:id:361 from text/plain to application/pdf"
read 73 bytes
reading 2 bytes...
-> ""
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn close

with“text / plain”

opening connection to stage.rest.click2mail.com...
opened
<- "GET /v1/mailingBuilders/54544/proofs/1 HTTP/1.1\r\nAccept: text/plain\r\nAuthorization: Basic Ym9sb2RldjptVW43Mjk0eQ==\r\nConnection: close\r\nHost: stage.rest.click2mail.com\r\n\r\n"
-> "HTTP/1.1 406 Not Acceptable\r\n"
-> "Date: Sun, 06 May 2012 14:14:19 GMT\r\n"
-> "Server: Jetty(6.1.x)\r\n"
-> "Content-Length: 0\r\n"
-> "Connection: close\r\n"
-> "Content-Type: text/plain; charset=UTF-8\r\n"
-> "\r\n"
reading 0 bytes...
-> ""
read 0 bytes
Conn close

从curl命令登录

修改(4)

我找到了Rest Client的解决方案,我用这个宝石做了我的微薄贡献。

https://github.com/bolom/click2mail-ruby-gem

谢谢每个人

3 个答案:

答案 0 :(得分:1)

你也可以使用net :: http(require'net / http') 有关如何下载大文件的示例,请参阅this question

答案 1 :(得分:0)

试试这个:

body = Httparty.get("https://username:password@rest.click2mail.com/v1/mailingBuilders/456/proofs/1")

File.open("myProof", "w") do |file|
    file.write body
end

答案 2 :(得分:0)

问题在于它是自己的API。

它与您如何调用API以获取证据或您正在使用的Rest API库无关。问题是你用来创建这个mailBuilders的任何问题导致了一个问题,导致错误消息“无法将urn:c2m:document:id:361从text / plain转换为application / pdf”。

请发送support@click2mail.com确切地说你已经做了什么来创建这个mailingBuilder,这样我们就可以查看它,看看问题是什么。