2

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!

  • Normally the charset is assigned with =, so you could try application/x-www-form-urlencoded;charset=Windows-1252 – birgire Jun 11 '17 at 11:11
  • I noticed that according to this list the Content-Type is a response field, not a request field. I thought it was also valid as a request field? There we see e.g. the Accept-Charset request field, but I'm don't think that's what you need. Have you tried encoding the POST data manually ? – birgire Jun 12 '17 at 16:11
  • 1
    @birgire you're totally right, and that's what I was using, the snippet here is incorrect but on my source code it is correct. Just fixed the typo on my question. – Diego de Oliveira Jun 12 '17 at 16:14
  • @birgire actually on the link you sent the Content-Type is also a request field. And to define the charset, it seems that this is the correct approach: w3.org/International/articles/http-charset/index.en – Diego de Oliveira Jun 12 '17 at 16:19
  • 1
    @birgire thanks for the help, your tip about encoding the POST data, I found a workaraound! See my answer below. – Diego de Oliveira Jun 18 '17 at 19:56
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.