[R] writing binary data from RCurl and postForm

Martin Morgan mtmorgan at fredhutch.org
Wed Aug 5 23:49:42 CEST 2015


On 08/05/2015 11:52 AM, Greg Donald wrote:
> I'm using RCurl with postForm() to post to a URL that responds with a
> PDF.  I cannot figure out how to write the resulting PDF data to a
> file without corruption.
>
> result = postForm(url, binary=TRUE)
>
> Both this:
>
> capture.output(result, file='/tmp/export.pdf')
>
> and this:
>
> f = file('/tmp/export.pdf', 'wb')
> write(result, f)
> close(f)
>
> result in a corrupted PDF.
>
> I also tried postForm without binary=TRUE but that halts execution
> with an "embedded nul in string" error.
>
> I also tried writeBin() but that complains about my result not being a vector.

I think that is because the value returned from postForm has an attribute; 
remove it by casting the return to a vector

   fl <- tempfile(fileext=".pdf")
   writeBin(as.vector(postForm(url, binary=TRUE)), fl)


The httr package might also be a good bet

   writeBin(content(POST(url)), fl)

>
> I can use curl on the command line and this works fine, but I need to
> get this working in R.  Any help would be greatly appreciated.
>
> Thanks.
>
> --
> Greg Donald
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-help mailing list