Skip to content
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

gloo net add .json convenience method for Request #199

Closed
databasedav opened this issue Mar 16, 2022 · 4 comments
Closed

gloo net add .json convenience method for Request #199

databasedav opened this issue Mar 16, 2022 · 4 comments

Comments

@databasedav
Copy link

Summary

add .json convenience method for Request

Motivation

makes sending json parameters more ergonomic

Detailed Explanation

right now, we must do a .body(serde_json::to_string(&JsonSerializableStruct { ... }).unwrap()), which isn't as ergonomic for an http library as, for example, reqwest with which you can do .json(&JsonSerializableStruct { ... })

Drawbacks, Rationale, and Alternatives

i don't think there are drawbacks, refer to reqwest source for implementation

Unresolved Questions

@databasedav databasedav changed the title gloo net add .json convenience method for Request gloo net add .json convenience method for Request Mar 16, 2022
@databasedav
Copy link
Author

you also have to add an .header("Content-Type", "application/json") right now, which would be embedded in the method

@ranile
Copy link
Collaborator

ranile commented Mar 18, 2022

Would you like to PR an implementation?

@minauteur
Copy link
Contributor

minauteur commented Mar 30, 2022

If something like the following would suffice (not sure given that the surrounding methods return Self rather than Result), I can start adding some tests and open a PR:

    /// convenience method for json request bodies
    pub fn json<T: Serialize + ?Sized>(self, value: &T) -> Result<Self,Error> {
        let json = serde_json::to_string(value)?;
        Result::Ok(
            self.header("Content-Type", "application/json")
            .body(json)
        )
    }

It's been a bit since I've done any Rust--apologies if that is bad 😬

@minauteur
Copy link
Contributor

This can probably be closed, as the requested change was added in #204

@ranile ranile closed this as completed May 25, 2022
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

No branches or pull requests

3 participants