Skip to content

Conversation

@tcharding
Copy link
Member

@tcharding tcharding commented Oct 26, 2025

This feature was removed when we gutted minreq after forking it. Turns out we need it to be able to use bitreq in jsonrpc.

Re-implement the feature by copying code from minreq.

Close: #393

@tcharding tcharding changed the title Re-implement json-using-serde feature bitreq: Re-implement json-using-serde feature Oct 27, 2025
/// ```no_run
/// use serde_json::Value;
///
/// # fn main() -> Result<(), minreq::Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// # fn main() -> Result<(), minreq::Error> {
/// # fn main() -> Result<(), bitreq::Error> {

/// # fn main() -> Result<(), minreq::Error> {
/// # let url_to_json_resource = "http://example.org/resource.json";
/// // Value could be any type that implements Deserialize!
/// let user = minreq::get(url_to_json_resource).send()?.json::<Value>()?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// let user = minreq::get(url_to_json_resource).send()?.json::<Value>()?;
/// let user = bitreq::get(url_to_json_resource).send()?.json::<Value>()?;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these not caught in CI? I get an error locally. Ah, is it because of #400 not being merged yet?

let original_json: serde_json::Value = serde_json::from_str(JSON_SRC).unwrap();
let response = bitreq::post(url("/echo")).with_json(&original_json).unwrap().send().unwrap();
let actual_json: serde_json::Value = response.json().unwrap();
assert_eq!(&actual_json, &original_json);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_eq!(&actual_json, &original_json);
assert_eq!(actual_json, original_json);

Comment on lines 160 to 166
let str = match self.as_str() {
Ok(str) => str,
Err(_) => return Err(Error::InvalidUtf8InResponse),
};
match serde_json::from_str(str) {
Ok(json) => Ok(json),
Err(err) => Err(Error::SerdeJsonError(err)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let str = match self.as_str() {
Ok(str) => str,
Err(_) => return Err(Error::InvalidUtf8InResponse),
};
match serde_json::from_str(str) {
Ok(json) => Ok(json),
Err(err) => Err(Error::SerdeJsonError(err)),
serde_json::from_str(self.as_str()?)
.map_err(Error::SerdeJsonError)

The existing code returns the incorrect error variant. The docs say InvalidUtf8InBody which is what as_str() returns.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half right I think. Your suggestion is correct and better but AFAICT it doesn't change the logic. I used the suggestion.

This feature was removed when we gutted `minreq` after forking it.
Turns out we need it to be able to use `bitreq` in `jsonrpc`.

Re-implement the feature by copying code from `minreq`.

After copy, fix docs to mention `bitreq` and also refactor some error
logic to use `as_str()?` directly (no logic change).
Copy link
Collaborator

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 99c948c

I've tested it locally, and with the rust-esplora-client PR (see bitcoindevkit/rust-esplora-client#137), and it works just fine.

Copy link
Collaborator

@jamillambert jamillambert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 99c948c

@tcharding tcharding merged commit b68f20d into rust-bitcoin:master Oct 30, 2025
30 checks passed
tcharding added a commit to tcharding/corepc that referenced this pull request Oct 30, 2025
99c948c Re-implement json-using-serde feature (Tobin C. Harding)
1251953 Replace stale docs reference to minreq (Tobin C. Harding)

Pull request description:

  This feature was removed when we gutted `minreq` after forking it. Turns out we need it to be able to use `bitreq` in `jsonrpc`.

  Re-implement the feature by copying code from `minreq`.

  Close: rust-bitcoin#393

ACKs for top commit:
  oleonardolima:
    tACK 99c948c
  jamillambert:
    ACK 99c948c

Tree-SHA512: 2e22881566fd3d75e0edb70567dac0b1ed9ce66720511dcead888b2b7686fc89fb1f6bce3bd490f91997e9ade88b0665b00679c79bcef35205c6ce1117306faf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bitreq: Add json support

3 participants