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

Networking: Preserve the content-type header when fetch()-ing #2028

Merged
merged 3 commits into from
Nov 26, 2024

Conversation

adamziel
Copy link
Collaborator

Allows the use of arbitrary Content-type header when sending POST requests from WordPress.

Playground fetch() network transport for wp_safe_remote_* calls enforced the Content-Type header of application/x-www-form-urlencoding when POST-ing. This PR turns that value into a fallback that's only used if not explicit value is provided.

Closes #1428

Testing instructions

  • CI unit tests
  • Try the Blueprint URL below and confirm in devtools the content-type header used was text/plain
http://localhost:5400/website-server/#{%22features%22:{%22networking%22:true},%22steps%22:[{%22step%22:%22writeFile%22,%22path%22:%22/wordpress/wp-content/mu-plugins/0-fetch-test.php%22,%22data%22:%22%3C?php%20add_action('init',%20function()%20{%20wp_safe_remote_post('https://api.w.org',array('headers'=%3E['Content-Type'=%3E'text/plain']));%20});%20%22}]}

Allows the use of arbitrary Content-type header when sending POST
requests from WordPress.

Playground fetch() network transport for `wp_safe_remote_*` calls
enforced the `Content-Type` header of `application/x-www-form-urlencoding`
when POST-ing. This PR turns that value into a fallback that's only used
if not explicit value is provided.

Closes #1428

 ## Testing instructions

* CI unit tests
* Try the Blueprint URL below and confirm in devtools the content-type
  header used was `text/plain`

```
http://localhost:5400/website-server/#{%22features%22:{%22networking%22:true},%22steps%22:[{%22step%22:%22writeFile%22,%22path%22:%22/wordpress/wp-content/mu-plugins/0-fetch-test.php%22,%22data%22:%22%3C?php%20add_action('init',%20function()%20{%20wp_safe_remote_post('https://api.w.org',array('headers'=%3E['Content-Type'=%3E'text/plain']));%20});%20%22}]}
```
@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Aspect] Networking labels Nov 25, 2024
Copy link
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

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

Looks good to me. I pushed a fix for the lint error in the tests and left an opinion about the form of header check. Thanks!

@adamziel adamziel merged commit 2775e68 into trunk Nov 26, 2024
10 checks passed
@adamziel adamziel deleted the preserve-content-type-when-fetching branch November 26, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Aspect] Networking [Type] Bug An existing feature does not function as intended
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

wp_remote_post Content-Type header not respected
2 participants