2012-05-06 77 views
0

我試圖將此轉換:如何將帶有輸出選項的curl命令轉換爲httparty?

curl -k -v -X GET -H "Accept: application/pdf" https://username:[email protected]/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}" 

響應返回

「無法轉換甕:C2M:文檔:ID:361從純文本/應用/ PDF」

編輯(2)

body.inspect與 「text/plain的」 返回

#<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\"]}> 

與 「應用程序/ 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選項

與 「應用程序/ 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 

從捲曲命令 「文本/純」

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 

日誌

編輯(4)

那麼我發現了一個與Rest客戶解決方案,我做了我對這個寶石的適度貢獻。

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

感謝每一個身體

+0

順便說一句,你已經包括在主體這裏您的用戶名/密碼('Authorization'頭)。 – Ashe

+0

@Len與RestClient。我沒有這個問題。感謝您的幫助 – Bolo

+0

做得好!很高興聽到你的工作。 – Ashe

回答

0

試試這個:

body = Httparty.get("https://username:[email protected]/v1/mailingBuilders/456/proofs/1") 

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

api不允許沒有文件名的請求。 parsed_response返回此 「無法轉換甕:從純文本/應用/ PDF 361 C2M:文件:id爲」 – Bolo

+0

我不知道你在說些什麼現在。 :| – Ashe

+0

您的解決方案不起作用。 p body.parsed_response返回我這個「」不能轉換甕:C2M:文檔:ID:361從純文本/應用/ PDF」。 – Bolo

1

您也可以使用網:: HTTP(需要 '網/ HTTP')中的示例 見this question如何下載大文件。

+0

它不起作用。該請求需要一個文件名。 – Bolo

+0

如果我們要幫助你,你需要解釋你看到這個錯誤的地方。 – Ashe

+0

here:p body.parsed_response – Bolo

0

問題出在API本身。

它與您如何調用API以獲取證明或您正在使用的Rest API庫無關。問題是無論你用什麼創建這個mailingBuilders導致了一個問題,導致錯誤信息「無法將urn:c2m:document:id:361從文本/純文本轉換爲application/pdf」。

請發送支持@ click2mail。com正是你爲創建這個mailingBuilder所做的,所以我們可以檢查它,看看問題是什麼。