Make WordPress Core

Opened 4 years ago

Closed 4 months ago

Last modified 6 weeks ago

#54556 closed defect (bug) (worksforme)

It's impossible to get 'location' header on 302 redirect of wp_remote_post

Reported by: saturnix2025's profile saturnix2025 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

https://wordpress.stackexchange.com/questions/398738/get-returned-url-from-wp-remote-post-if-response-code-is-302

<?php
$payload = array('body' => $payload);
$payload['redirection'] = 0;
$response = wp_remote_post( $my_url, $payload );
$redirect_to = wp_remote_retrieve_header( $reponse, 'location' );

In the above code, $redirect_to always returns an empty string when $my_url is an URL that returns 302 redirect.

This is unexpected behavior, as $redirect_to should contain the URL $my_url is redirecting us to.

I've tried replacing "location" with "Location" and "0" with "false" to no avail.

Change History (4)

#1 follow-up: @costdev
4 years ago

  • Keywords reporter-feedback added

Hi @saturnix2025, thanks for opening this ticket and welcome to Trac!

Trac is for reporting issues with WordPress Core - so let's see if that's where this issue lies.

  • What version of WordPress are you using?
  • What is the output of var_dump( $response );?

#2 in reply to: ↑ 1 @saturnix2025
4 years ago

Replying to costdev:

Hi @saturnix2025, thanks for opening this ticket and welcome to Trac!

Trac is for reporting issues with WordPress Core - so let's see if that's where this issue lies.

  • What version of WordPress are you using?
  • What is the output of var_dump( $response );?

Unfortunately I don't know, as this is shared hosting with no access to a dev console.

Version is 5.8.2

Last edited 4 years ago by saturnix2025 (previous) (diff)

#3 @mindctrl
4 months ago

  • Keywords reporter-feedback removed
  • Resolution set to worksforme
  • Status changed from new to closed

Hi all,

I've tried to replicate the issue, but it works as expected for me on WP 6.8.3.

Here is my environment info:

=== Environment
- WordPress: 6.8.3
- PHP: 8.4.10
- Server: nginx/1.26.1
- Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.4.10)
- Browser: Chrome 140.0.0.0
- OS: macOS
- Theme: Twenty Twelve 4.6
- MU Plugins:
  * z.php 
- Plugins:
  * Test Reports 1.2.0

=== Actual Results
1.  ❌ Error condition does not occur.

=== Additional Notes

Nginx config:
location = /trac54556 {
    return 302 /new-trac54556;
}

location = /new-trac54556 {
    return 200 "Hi";
}


Code used to confirm redirect URL is returned:

$response = wp_remote_get( "http://vanilla.local/trac54556", [ 'redirection' => 0 ] );
$redirect_to = wp_remote_retrieve_header( $response, 'location' );
echo '<pre>$redirect_to<br>' . print_r( $redirect_to, true ) . '</pre>';

Returns: http://vanilla.local:10008/new-trac54556

Since this ticket has been opened for ~4 years with no other reports, I'm going to close this to help clean up Trac. If this is still an issue for you, please feel free to reopen and provide any environmental information necessary to reproduce.

#4 @swissspidy
6 weeks ago

  • Milestone Awaiting Review deleted

Removing milestone from closed ticket.

Note: See TracTickets for help on using tickets.