How to set charset for wp_remote_post request?

I’m using wp_remote_post() on a project and it works great. But seems like the server on which I need to make the POST request is a Windows (ISS) server. So the third-party service that provides this specific endpoint requires that all POST requests has a Windows-1252 character encoding, otherwise there will be issues with special characters (like á, é, û, etc).

I did some research and seems like the way to go is to set a Content-Type HTTP header which includes a charset (like charset=Windows-1252). Tried to add the charset along with the content type (like application/x-www-form-urlencoded;charset=Windows-1252), but nothing seems to work.

Does anyone know how to proper set the charset for an HTTP POST request using wp_remote_post()?

Thanks in advance!

1 Answer
1

After trying several times to set the proper charset, I wasn’t successful. Then I searched for an workaround and found one that worked with my problem.

In addition to set the charset like I mentioned above, what I did is to convert all strings on my POST body with this php snippet:

$message = @iconv("UTF-8","Windows-1252//IGNORE",$message);

This converts all of your characters into compatible Windows-1252 strings. For me, it worked, and all special characters are properly recognizable on the ISS server.

Leave a Comment