[R] help with sockets in R

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Sep 22 08:43:57 CEST 2010


readLines() is for a text-mode connection; readChar() is for a 
binary-mode connection.  Given that you asked for possible re-encoding 
by the 'encoding' argument, you cannot safely mix them (text-mode 
access is re-encoded, binary-mode is not).  However, we don't know if 
re-encoding was active in your case since we don't know your locale.

Either don't specify an encoding and re-encode the response in R or 
use readLines() to read the complete response and split it up later.

For a different approach with read/write.socket() see tests/internet.R 
in the R sources.

Please do note that the posting guide asked you for 'at a minimum' 
information (which includes the locale) and a reproducible example.

On Tue, 21 Sep 2010, Christopher Bare wrote:

> Hi R gurus,
>
> I'm trying to use a ReSTful web service from within R. Specifically, I
> need to make HTTP PUT requests.
>
> I'm able to make the request and that goes well enough, but I'm having
> trouble properly consuming the HTTP response. The problem comes in
> when I'm trying to parse out the response body. I get the length of
> the response body from the Content-Length header. I then try to use
> readChar(con, nchars=content.length).
>
> The result I'm seeing is that the first few characters of the response
> body are cut off.
>
> My code looks like this:
>
>
> http.request <- function(host, path, request, port=80) {
>
> 	con <- socketConnection(host=host, port=port, open="w+",
> blocking=TRUE, encoding="UTF-8")
> 	writeLines(request, con)
>
> 	write("wrote request", stderr())
> 	flush(stderr())
>
> 	# build response object
> 	response <- list()
> 	response$status <- readLines(con, n=1)
> 	response$headers <- character(0)
> 	repeat{
> 		ss <- readLines(con, n=1)
> 		write(ss, stderr())
> 		flush(stderr())
> 		if (ss == "") break
> 		key.value <- strsplit(ss, ":\\s*")
> 		response$headers[key.value[[1]][1]] <- key.value[[1]][2]
> 	}
>
> 	if (any(names(response$headers)=='Content-Length')) {
> 		content.length <- as.integer(response$headers['Content-Length'])
> 		response$body <- readChar(con, nchars=content.length)
> 	}
> 	close(con)
> }
>
>
> response$body ends up with
>
> "e,\"id\":\"some_doc\",\"rev\":\"7-906e06a7744780ef93126adc6f8f10ef\"}\n"
>
> when it should be:
>
> "{\"ok\":true,\"id\":\"some_doc\",\"rev\":\"7-906e06a7744780ef93126adc6f8f10ef\"}"
>
>
> Is mixing readLines and readChars on the same connection causing bad
> juju? If it is, what's the recommended what to do such a thing?
>
>
> Thanks for any help!!
>
> -Chris
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list