Giter Club home page Giter Club logo

Comments (9)

jeroen avatar jeroen commented on May 22, 2024

See curl_fetch_memory

from curl.

sebastian-c avatar sebastian-c commented on May 22, 2024

Ahah, thanks. I can use the unexported handle_response_data. Is there a reason that this isn't exported?

from curl.

sebastian-c avatar sebastian-c commented on May 22, 2024

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?:

  1. Establish curl connection.
  2. a) If the connection fails (non 200), use readLines to get the response and then parse it.
  3. b) If the connection succeeds, continue to use stream_in to read it.

from curl.

jeroen avatar jeroen commented on May 22, 2024

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.

sebastian-c avatar sebastian-c commented on May 22, 2024

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.

jeroen avatar jeroen commented on May 22, 2024

As of curl 2.0 the function handle_data is exported which gives you the handle state.

from curl.

sebastian-c avatar sebastian-c commented on May 22, 2024

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.

jeroen avatar jeroen commented on May 22, 2024

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.

sebastian-c avatar sebastian-c commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.