0

I have an excel file with a button associated to a VB macro like this:

Sub button_macro()
    Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    MyRequest.Open "GET", "http://theurl/service?param=""Gestión"""
    MyRequest.Send
End Sub

But the response I get is something like:

"Gestión" is not a valid value for param.

How can I avoid VB converting the 'ó' character to another encoding?

If I send the request via browser like:

http://theurl/service?param="Gestión"

The service answer as desired.

EDIT:

Curiously MsgBox "Código único" works as expected showing the 'ó' and 'ú' characters correctly.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
  • Does it work without the quotes? I've never seen quoted querystring parameters... – Tim Williams Nov 16 '20 at 19:01
  • The real use case contains several words for param, like "Gestión CAR", so I must enclose them in double quotes (the service doesn't work other way). – Paco Abato Nov 16 '20 at 19:03
  • https://stackoverflow.com/questions/50182394/making-a-utf-8-call-from-vba and maybe https://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba (you likely need the UTF-supporting one from the accepted answer) – Tim Williams Nov 16 '20 at 19:09
  • FYI it works in the browser because the browser encodes the request for you - you can see that if you use your browser's Developer tools - eg for your "example" URL of `http://theurl/service?param="Gestión"` I see `http://theurl/service?param=%22Gesti%C3%B3n%22` – Tim Williams Nov 16 '20 at 19:10
  • I see, using "Gesti%C3%B3n" works as expected. Thanks! – Paco Abato Nov 16 '20 at 19:12
  • 1
    Please, try something like this: `myRequest.Open "GET", "http://theurl/service?param=" & WorksheetFunction.EncodeURL("""Gestión""")`. It should work with any string containing characters necessary to be encoded. – FaneDuru Nov 16 '20 at 19:30

1 Answers1

1

It works in the browser because the browser encodes the request for you - you can see that if you use your browser's Developer tools - eg for your "example" URL of

http://theurl/service?param="Gestión" 

I see

http://theurl/service?param=%22Gesti%C3%B3n%22

You can encode your parameters using (eg) the UTF-supporting method from the accepted answer here: How can I URL encode a string in Excel VBA?

Tim Williams
  • 154,628
  • 8
  • 97
  • 125