-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http.send fails to yield a response body if it is not JSON #1258
Comments
@agurney thanks for filing this. The I think we should do a few things...
I think we'll need to keep an eye on this as I'm not sure how well binary data will get handled. |
@ashutosh-narkar can you take a look at this? |
Yes I will look into this. |
Thanks for jumping on this so speedily! Taking a look at the PR now. |
Expected Behavior
The language reference describes
http.send
as simply returning the response body as a component ofoutput
. It sounds from the documentation that thisbody
member ought to be a string, which could then be further processed withjson.unmarshal
if it's JSON, or otherwise if it's not.Actual Behavior
If the HTTP server response is parsable as JSON, that appears in
output.body
as a term, i.e. unmarshaled from a string. If the HTTP server responds with a body that doesn't contain valid JSON, what we get isnull
. Theoutput
has nothing to indicate what happened to the body.See
opa/topdown/http.go
Line 275 in 0d9cd4a
Steps to Reproduce the Problem
Invoke
http.send
against aurl
that will return a non-JSON response payload.Additional Info
If nothing else, the documentation should describe the current state of affairs. This would make it easier to write Rego using
http.send
since it would be more obvious what can be done with theoutput
, and which kinds of responses can be handled in general.For speaking to non-JSON endpoints, one possibility is to add a
raw_body
member tooutput
. This would preserve the existing behavior while also providing a more general capability for non-JSON data.Another would be to fall back to
body
containing the raw response body if JSON parsing fails, or if theContent-type
header doesn't indicate JSON. But it is probably confusing ifbody
is sometimes parsed JSON and sometimes a raw string, and theContent-type
is unfortunately not always reliable in the wild.The idea in #1176 (comment) also seems interesting in the longer term, replacing the low-level
http.send
facility with a higher-level abstraction over remote data sources.The text was updated successfully, but these errors were encountered: