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

content-type in FormData for string part #45037

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BadLice
Copy link

@BadLice BadLice commented Jun 18, 2024

Summary:

When performing an xhr request with content-type: multipart/form-data, it is sometimes required to specify a content-type header for a string part of the FormData. Currently, this is not possible, because the FormData only sets this header for a Blob part. This PR addresses this issue, allowing to append a part as "string object" with an optional type property that will be used by FormData.getParts as content-type headers, like it happens for Blob right now.

Changelog:

[GENERAL] [ADDED] - FormData.append function can now accept a "string object" with an optional type parameter that will be used by FormData.getParts function as content-type header

Test Plan:

before the changes:
there was no way to specify the content-type as application/json

const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://v2.convertapi.com/upload');
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
const formData = new FormData();
formData.append('sampleName', '{sampleProperty:"sample text value"}');

this produced the following request body:

--Q2nvVBuNTRU0yH8YBt-j_qrkfK6mVPu.8fNLbj8c4DUoBEhfgN5HuVjbZL4VaAB.vlA3k2
content-disposition: form-data; name="sampleName"

{sampleProperty:"sample text value"}
--Q2nvVBuNTRU0yH8YBt-j_qrkfK6mVPu.8fNLbj8c4DUoBEhfgN5HuVjbZL4VaAB.vlA3k2--

after the changes, it is possible to eventually specify the type of the appended part like this:

const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://v2.convertapi.com/upload');
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
const formData = new FormData();
formData.append('sampleName', {string: '{sampleProperty:"sample text value"}', type:'application/json'});

resulting in the following request body, with the correct content-type:

--dqHX9tecYDsMgYRgJ17SNyk62wxNEG0bnB1k-JKF9_XI4lxGUDS0u8xdx6zGr.Fmf9bHKx
content-disposition: form-data; name="sampleName"
content-type: application/json

{sampleProperty:"sample text value"}
--dqHX9tecYDsMgYRgJ17SNyk62wxNEG0bnB1k-JKF9_XI4lxGUDS0u8xdx6zGr.Fmf9bHKx--

it is still possible to pass directly a string like in the first example, the behaviour will be the same as before the changes

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Jun 18, 2024
@BadLice BadLice force-pushed the custom-headers-in-form-data branch from 32f6ec1 to 05cc795 Compare June 18, 2024 16:08
@BadLice BadLice marked this pull request as draft June 18, 2024 16:16
@BadLice BadLice force-pushed the custom-headers-in-form-data branch 2 times, most recently from a259aa1 to fcd3e07 Compare June 18, 2024 16:39
@BadLice BadLice marked this pull request as ready for review June 18, 2024 16:42
@BadLice BadLice force-pushed the custom-headers-in-form-data branch from fcd3e07 to 00bab35 Compare June 19, 2024 08:08
@BadLice BadLice marked this pull request as draft June 19, 2024 08:22
@BadLice BadLice force-pushed the custom-headers-in-form-data branch 3 times, most recently from 33d23a9 to bfbf83c Compare June 19, 2024 09:35
… a `content-type` header for a string part using the `type` property in the string object. `getParts` function now parses string objects as strings with `content-type` if any
@BadLice BadLice force-pushed the custom-headers-in-form-data branch from bfbf83c to d0362c8 Compare June 19, 2024 09:54
@BadLice BadLice marked this pull request as ready for review June 19, 2024 09:54
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 20,460,268 +25
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 23,658,926 +19
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: d8739e1
Branch: main

@BadLice BadLice changed the title Custom headers in form data content-type in FormData for string part Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants