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

Add support to stringify automatically the body of the request if the type of it isn't a string #24

Merged
merged 1 commit into from
Jun 18, 2021

Conversation

marcelolx
Copy link
Collaborator

References #23

This allows us the make the request sending a javascript object and then RequestJS does the job of stringify the body.

I think Request.JS can do this, since most of the packages who wrap the Fetch API do the same.

@marcelolx marcelolx added the enhancement New feature or request label Jun 17, 2021
@marcelolx marcelolx requested a review from dhh June 17, 2021 23:07
@marcelolx marcelolx changed the title Add support for stringify automatically the body of the request if the type of it isn't a string Add support to stringify automatically the body of the request if the type of it isn't a string Jun 17, 2021
Copy link
Contributor

@adrienpoly adrienpoly left a comment

Choose a reason for hiding this comment

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

I really like this idea to make it very simple for the user to just pass an object and it works.

One little comment for an alternative implementation that could be more inline with the current code style

src/fetch_request.js Outdated Show resolved Hide resolved
… type of it isn't a string

This allows us the make the request sending a javascript object and then RequestJS does the job of stringify the body
...

async myMethod () {
const response = await post('localhost:3000/my_endpoint', { body: { name: 'Request.JS' } })
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this need responseKind: 'json' to trigger the stringify?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What is required is the Content-Type to be application/json and this is the default content type in this scenario https://github.com/rails/request.js/blob/main/src/fetch_request.js#L73

Copy link
Contributor

@excid3 excid3 Jun 18, 2021

Choose a reason for hiding this comment

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

Ah! I didn't catch json as the default content type. 👍

Haven't had any caffeine yet this morning. 😅

@marcelolx marcelolx merged commit 56f1903 into rails:main Jun 18, 2021
@marcelolx marcelolx deleted the stringify branch June 18, 2021 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants