-1

I use the Java class that Twitter provides in its GitHub for the filtered stream endpoint (https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/FilteredStreamDemo.java) and it works fine.

However, when I add in a rule a word that contains non ASCII characters (for example the word "siccità") it returns the error:

{"errors":[{"parameters":{},"message":"Invalid JSON"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}

I tried to convert the word to UTF-8 but it did not work at all. Is there any way to add words with non ASCII characters to the Twitter API v2 rules in Java?

Nick Pantelidis
  • 455
  • 4
  • 12
  • 1
    ```httpGet.setHeader("Content-Type", "application/json; charset=UTF-8");``` might make a difference. Seemingly the default encoding of HttpClient is ISO-8859-1 – g00se Sep 08 '22 at 14:16
  • "it did not work at all" - was there an error from the API? Also, I wonder whether you have tried the same rule but (if possible) with one of the samples in a different coding language in that same GitHub repo, or submitting the rule via Postman or curl? – Andy Piper Sep 08 '22 at 14:24
  • @g00se I tried it but it did not work. Thanks for the suggestion – Nick Pantelidis Sep 08 '22 at 18:45
  • It might help to know *exactly* what you tried – g00se Sep 08 '22 at 19:17
  • I tried to add httpGet.setHeader("Content-Type", "application/json; charset=UTF-8"); as you suggested and it did not work – Nick Pantelidis Sep 09 '22 at 06:23
  • @AndyPiper the problem exists only in Java – Nick Pantelidis Sep 09 '22 at 06:46

1 Answers1

1

In the class FilteredStreamDemo.java that Twitter provides here: https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/FilteredStreamDemo.java you have to go to the class createRules and replace this line of code:

StringEntity body = new StringEntity(getFormattedString("{\"add\": [%s]}", rules));

with that:

StringEntity body = new StringEntity(getFormattedString("{\"add\": [%s]}", rules), "UTF-8");

and it will work fine even when there are non ASCII characters in the rules.

Nick Pantelidis
  • 455
  • 4
  • 12