Comments (9)
See curl_fetch_memory
from curl.
Ahah, thanks. I can use the unexported handle_response_data
. Is there a reason that this isn't exported?
from curl.
Ah, but then I don't get the content... Is there currently a way to do this with curl
or a similar connection
so that streaming is possible using jsonlite::stream_in
? Alternatively, is there a way to implement logic like the following?:
- Establish
curl
connection. - a) If the connection fails (non 200), use readLines to get the response and then parse it.
- b) If the connection succeeds, continue to use
stream_in
to read it.
from curl.
The curl_fetch_memory
and curl_fetch_disk
functions are basically wrappers for handle_response_data
plus content.
Why do you want to parse the content of a failed request? It is probably empty, or an error page or so. What would you do with that, other than raising an error?
from curl.
In my particular case I'm making POST requests to a server and it responds with what was wrong with my query (for example the column I'm trying to filter by doesn't exist, filtering by an invalid value, etc.)
from curl.
As of curl 2.0 the function handle_data
is exported which gives you the handle state.
from curl.
Sorry for replying late to this, but I managed to solve my issue using internal functions and now, naturally, they've broken - my fault, foolish. Consider the following case where I get an error, but still want a response. Pretend for a moment that instead of a teapot, I'm after an error message that the server returns telling me what was wrong with my data:
# Use curl 2.3 for this, 2.4 onwards will crash R
library(curl)
url <- "http://httpbin.org/status/418"
json <- "{\"a\" : \"b\"}"
h <- new_handle()
handle_setopt(h, customrequest="POST",
verbose = FALSE,
post = 1,
postfields = json)
conn <- .Call(curl:::R_curl_connection, url, "r", h, FALSE)
responseCode <- handle_data(h)$status_code
if(responseCode != 200){
errmessage <- paste0(readLines(conn, warn = FALSE), collapse="\n")
cat(errmessage)
}
This message is not accessible using curl
without the internal function:
h2 <- new_handle()
handle_setopt(h2, customrequest="POST",
verbose = FALSE,
post = 1,
postfields = json)
conn2 <- curl(url, open = "r", handle = h2)
responseCode <- handle_data(h2)$status_code
if(responseCode != 200){
errmessage <- paste0(readLines(conn2, warn = FALSE), collapse="\n")
}
Note that the response is not contained in the handle_data(h2)$headers
object.
from curl.
Hi @sebastian-c, I have added a new feature for you in the latest version of curl:
source("https://install-github.me/jeroen/curl")
If you add the letter f
to open()
the connection will be opened without raising an error for non-success http status codes. Does this solve your problem?
h <- new_handle()
con <- curl(httpbin("status/418"), handle = h)
open(con, "rf")
out <- readLines(con)
handle_data(h)$status_code
close(con)
from curl.
I just tested it with my code - It works really well. I think that was the last internal curl
function I was using. My code should be more robust to future updates now and I hope it's a useful addition in general (I understand that you're trying to be very conservative with this package).
Thank you.
from curl.
Related Issues (20)
- Cannot use the correct curl library on Mac HOT 6
- Differences between POSTing with the CLI curl (Bash) and the R curl package HOT 1
- can not install curl Rpackage HOT 1
- Error in curl::curl_fetch_memory(url, handle = handle) HOT 2
- Do discovered vulnerabilities in curl apply here? HOT 1
- Issues with postfieldsize in recent R curl versions
- 'Remote file not found' if SMB path has Unicode characters HOT 6
- Error in curl::handle_setopt(handle, .list = req$options): Option seekfunction (20167) has unknown or unsupported type. HOT 2
- form_data documentation is lacking
- parse_headers throws an error when a site responds with an empty header HOT 2
- Out of memory errors for libcurl ≥ 7.71.0 HOT 1
- FTP directory created successfuly, but curl returns with error HOT 3
- Feature request: Use environment variable `CURL_CA_BUNDLE` if set HOT 14
- inconsistent SSL version in vignette
- Allow users to pass pool object in `multi_download()`
- Use httpuv::randomPort() in curl_echo
- Why is curl_echo() slow sometimes HOT 2
- How to resolve "SSL: no alternative certificate subject name matches target host name '<proxy.server.address>'"
- httpuv delays in curl_echo on Linux HOT 1
- Can't get all bytes received before RST HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from curl.