Search code examples
excelvbahttpms-accessgmail-api

Do not encode UTF-8 characters in the query parameter in an http request


I'm using Tim Hall's WebRequest class to make http requests.
By default it encodes UTF-8 characters that are in the URL, in general this is correct behavior.

When I load from the Gmail API messages from the email box that contain a specific label, and the name of the label is in Hebrew, I must pass in the query parameter of the URL the name of the label in Hebrew, otherwise Gmail doesn't find a match, and returns 0 results.

I tried to make the same request with MSXML2.ServerXMLHTTP.6.0, where the address is not encoded and it did solve the problem.
However, I would like to do this with WebRequest because the entire structure of my application includes handling all types of http request errors already built on WebRequest.

Is it possible to prevent WebRequest from encoding the URL?

Dim request As New WebRequest
pGmailClient.BaseUrl = "https://www.googleapis.com/gmail/v1/"
request.Resource = "users/{userId}/messages"
request.AddUrlSegment "userId", "me"
request.AddQuerystringParam "q", "label:(בדיקה) is:(unread)"
request.AddHeader "Authorization", "Bearer " & pGmailClient.Authenticator.Token

Dim response As WebResponse
Set response = pGmailClient.Execute(request)

Here is the source of WebRequest: WebRequest


Solution

  • Have you tried something like

    request.Resource = "users/{userId}/messages?q=label:(בדיקה) is:(unread) ?

    Maybe just encode the space / parentheses

    Perhaps also look at this, which may be relevant here? Gmail API not respecting UTF encoding in subject