httr reset using POST

I'm using httr to retrieve data using api endpoints that pass through an authentication system.
I have to use httr::GET with the apiendopint ur, followed by httr::POST with my credentials.
and then
result <- httr::POST('https://internalcompanyserver/authentication, body = list(username, password), encode = 'form', httr::verbose())

This works fine.

But then when I want to retrieve data using a new set of parameters, the POST command with verbose set shows that it is querying the apiendpoint with the first set of parameters.
If I restart the R session, then run the GET and POST using the new set of parameters, it is fine.
Also, if I wait a sufficient amount of time (I didn't time it), something resets and I can then run GET and POST with the new set of parameters.

I need to be able to force a reset, because I am intending to user purrr::map to map through many parameter input changes to the apiendpoint.

Any assistance would be greatly appreciated.

From what you describe, you may be insterested in this advanced function: httr::handle_reset(url).

By default, httr will save some stuff to simplify the use of curl. httr is based on curl and you would need to create curl handle but httr does it for you and create a handle at first request and reuse it during the session. and usually this is what you want (for maintaining cookies for examples).
I am curious to understand why you need that by the way. Does your API follow a Oauth2 scheme for authentication ? What does your API documentation describe itself ?
There maybe another way to do what you want using httr.

If really you need advanced usage, you can use curl or crul directly or create yourself a curl handle to provide to handle argument in GET and POST so that you don't use the internal handle pool that seems to not be good for you.

hope it helps

1 Like


Thanks for the tips, that has been really helpful.
What I have ended up doing is using httr to reset the handle.
So, firstly, I had to find the correct url to use for the handle.

Which returned the required url to use in the handle
I can then set handle
custom_h <- httr::handle("https://someurl/")

adding the handle option to my previous GET and POST commands:
httr::GET('https://internalcompanyserver/apiendpoint_parameters', handle = custom_h)
result = httr::POST('https://inetrnalcompanserver/auththingy', body = login, encode = 'form', handle = custom_h)

Then I need to reset the handle by:
custom_h <- httr::handle("https://someurl/")

And then I can run new GET and POST commands.

I've found out that the api is using a basic authentication method (so not Oauth2).

1 Like

Glad it could help!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.