Comments (3)
Thank you for reporting this @opensourceame, I totally agree this is surprising!
Please take a look at this more detailed inspection of the two objects:
[19] pry(main)> Faraday::Response.new(status: 200, body: 'test')
=> #<Faraday::Response:0x0000000136195a78
@env=
#<struct Faraday::Env
method=nil,
request_body=nil,
url=nil,
request=nil,
request_headers=nil,
ssl=nil,
parallel_manager=nil,
params=nil,
response=nil,
response_headers=nil,
status=200,
reason_phrase=nil,
response_body="test">,
@on_complete_callbacks=[]>
[20] pry(main)>
[21] pry(main)> Faraday::Response.new(body: 'test', status: 200)
=> #<Faraday::Response:0x0000000116e26898
@env=
#<struct Faraday::Env
method=nil,
request_body="test",
url=nil,
request=nil,
request_headers=nil,
ssl=nil,
parallel_manager=nil,
params=nil,
response=nil,
response_headers=nil,
status=200,
reason_phrase=nil,
response_body=nil>,
@on_complete_callbacks=[]>
Why is this happening?
As you can see from above, body
is not an actual field in Faraday::Response
, but rather an alias for either request_body
or response_body
.
And this "dynamic aliasing" works based on status
: if status
is set, then we assume the response have been fetched and body
refers to response_body
. Otherwise, no response was fetched, so body
still refers to request_body
.
You can see that in how []
and []=
are implemented for Env
: https://github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb#L89-L105
When you generate the hashes in your examples, body
always refer to response_body
, because in both cases status
is set.
However, the former example sets response_body
, while the latter sets request_body
, hence the latter example shows body: nil
when you print it out.
Is this expected?
Although really convoluted, I'd say this is expected behaviour, because of the nature of the body
alias.
Users of the library won't normally instantiate a Faraday::Response
because the adapter will do that for them.
Adapters, OTOH, are encouraged to use the save_response
method which takes care of this for them among other things.
The real surprise here is that Faraday::Response
accepts body
as an initializer parameter, I'd have thought that would raise an error (instead, we should use request_body
or response_body
explicitly).
I'm currently planning some work on those Struct
s which may solve this confusion, so I'll leave this issue open until then.
from faraday.
Thanks for your response @iMacTia. I'm creating the response manually to create a dummy response when I get a callback from a remote server, which is passed to Ruby code via a queue system. Is there a better way to do this than initialising a response object, as in my example?
Agreed that body should not be accepted as an init param. Looking forward to your improvements ;-)
from faraday.
Is there a better way to do this than initialising a response object, as in my example?
I don't have a good alternative to that to be honest, but if you use a response_body
key instead of body
, at least you shouldn't have this issue anymore.
from faraday.
Related Issues (20)
- Strange behaviour with response HOT 1
- Setting ActiveRecord DatabaseResolver to enable multi database does not work with Faraday adapter HOT 3
- Unable to use webdav verbs HOT 3
- Change connection proxy HOT 2
- Drop Ruby < 3.0 support HOT 1
- `no_proxy` Parameter in Manual Proxy Configuration HOT 4
- connection headers are ignored when using build_response directly HOT 5
- Add tests and documentation to make sure adapters support streaming requests HOT 2
- Any solution for Digest Auth in Faraday 2? HOT 2
- Case Insensitivity Issue with #dig Method on Response Headers HOT 1
- Test friction HOT 2
- Is it possible when stubbing to execute the original request? HOT 2
- Allow not setting Content-Length when Transfer-Encoding header is set HOT 1
- Some API providers cannot interpret encoded colons in the path HOT 6
- Faraday removes preconfigured URI from instance on requests HOT 2
- Would you be open to me contributing a `#clear` method in `Faraday::Adapter::Test::Stubs`? HOT 1
- Support for chained client certificates? HOT 6
- Connecting to unix domain sockets HOT 3
- Add ciphers to SSL options HOT 6
- Async::HTTP::Faraday cannot use "in_parallel" HOT 6
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 faraday.