0

I am trying to convert this:

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

for httparty. Here's my code:

@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}"

the response returns

"Cannot convert urn:c2m:document:id:361 from text/plain to application/pdf"

Edit (2)

body.inspect with "text/plain" returns

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

Edit 3

Api : Step 8

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

Edit 4

with debug_ouput option

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

log from curl command

Edit (4)

Well i found a solution with Rest Client and I did my modest contribution with this gem.

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

Thanks Every body

Bolo
  • 2,668
  • 4
  • 27
  • 34

3 Answers3

1

You can also use net::http (require 'net/http') See this question for an example how to download large files.

Community
  • 1
  • 1
peter
  • 41,770
  • 5
  • 64
  • 108
  • You'll need to explain where you're seeing this error if we're to help you at all. – Asherah May 06 '12 at 10:53
  • here : p body.parsed_response – Bolo May 06 '12 at 10:59
  • @len I added few comments to my original question – Bolo May 06 '12 at 11:27
  • @Bolo, i tried my sample before i published, always do so it works, but you edited the question so now we haven more detail, i'll try this evening to do it with net/http – peter May 06 '12 at 12:18
  • @Bolo, to be able to test this i need a valid user/password, test/test obviously isn't working, have you a test account or something ? – peter May 06 '12 at 16:50
  • @Bolo, tried the user & password you used in the curl on pastie but it gives unauthorised – peter May 06 '12 at 19:31
0

Try this:

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

File.open("myProof", "w") do |file|
    file.write body
end
Asherah
  • 18,948
  • 5
  • 53
  • 72
  • the api doesn't allow a request without a file name. parsed_response returns this "Cannot convert urn:c2m:document:id:361 from text/plain to application/pdf" – Bolo May 06 '12 at 10:41
  • I have no idea what you're talking about now. :| – Asherah May 06 '12 at 10:52
  • your solution doesn't work. p body.parsed_response returns me this ""Cannot convert urn:c2m:document:id:361 from text/plain to application/pdf" – Bolo May 06 '12 at 10:52
  • You may need to add the `Accept` header to `Httparty`. Try `body = Httparty.get("...", :headers => {"Accept" => "application/pdf"})`. – Asherah May 06 '12 at 11:01
  • same thing with the header tag – Bolo May 06 '12 at 11:04
  • in fact without the option "-o", the request returns a response "nil" – Bolo May 06 '12 at 11:15
  • body.inspect returns this "#, @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\"]}>" – Bolo May 06 '12 at 11:22
  • @Bolo: you say it returns "nil" without "-o"—what, curl? That's clearly not true, curl is sending the same headers in both cases. You're omitting some information. We need to see the entire log of your requests, both working and not (have you checked the contents "myProof" when you did use `-o`?), and your Ruby attempts. – Asherah May 06 '12 at 13:01
0

The problem is with the API it's self.

It has nothing to do with how you are calling the API to get the proof or what Rest API library you are using. The problem is whatever you used to create this mailingBuilders is causing a problem which is resulting in the error message "Cannot convert urn:c2m:document:id:361 from text/plain to application/pdf".

Please send support@click2mail.com exactly what you have done to create this mailingBuilder so we can review it and see what the problem is.