Basic Authentication (or BasicAuth) is not natively handled with the WordPress HTTP API. This means when you’re using functions such as
wp_remote_post() there’s no immediately obvious way to send Basic Authentication headers with your request. It would be great to pass username and password parameters to these functions, but it’s not there.
Fear not though, it’s really easy. Here’s how:
$args = array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD ) ) ); wp_remote_request( $url, $args );
That’s it. The correct authentication headers will then be sent with your request (after you’ve replaced YOUR_USERNAME and YOUR_PASSWORD with the obvious).
I’d like to give a quick shout out to my favourite HTTP monitor Charles Proxy. I use Charles almost daily when dealing with server-side HTTP requests and AJAX requests and it makes life much easier. I love it.
Hi John, Thanks for the link to Charles. Just had a play and it looks incredibly useful.
A year later, but still great info thanks dude. Sometimes wordpress drives me crazy.
Nearly 6 years later, and this post has helped me out so much. Thank you for sharing dude!
Over 6 years.
7 years and counting. Thanks John :)
Comments are closed.