In this link, it has been explained how to use the Twitter search API, for example:
Your search URL is:
https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
when I do a GET call using the link from POSTMAN (setting OAuth 1 params)it works fine, but when i do the same GET call from JAVA, it gives me the following error:
Caused by: java.net.URISyntaxException: Illegal character in query at index 57: https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
it works fine if i just search with one word without space and OR.
It sounds like you need to encode the URL properly. For instance a space character must be replaced with %20. Here is a table of encodings. Really, you should use a built in function. See this thread.
Related
I'm trying to create an MUnit test that mocks an HTTP request by setting the payload to a JSON object that I have saved in a file. In Mule 3 I would have just done getResource('fileName.json').asString() and that worked just fine. In Mule 4 though, I can't statically call getResource.
I found a forum post on the Mulesoft forums that suggested I use MunitTools::getResourceAsString. When I run my test, I do see the JSON object but with all the \n and \r characters as well as a \ escaping all of the quotation marks. Obviously this means my JSON is no longer well formed.
Ideally I would like to find a reference for MunitTools so that I can see a list of functions that I can call and maybe find one that does not add the escape characters, but I haven't had any luck. If anybody knows of a some reference document that I can refer to, please let me know.
Not being able to find a way to return the data without the extra characters, I tried replacing them via dataweave. This is fine when replacing \n and \r, but as there are also more \s in front of each double quote and I can't seem to make these go away.
If I do this...
replace (/\/) with ("")
...I get an error. A co-worker suggested targeting the each \" and replacing them with ", but that's a problem because that gives me """. To get around this, I've tried
replace(/\"/) with "\""
...which does not cause any errors, but for some reason it reads the \ as a literal so it replaces the original string with itself. I've also tried...
replace(/\"/) with '"'
...but that also results in an error
I'm open to any other solutions as well.
Thanks
--Drew
I had the same concern so I started using the readUrl() method. This is a DataWeave method so you should be able to use it in any MUnit processor. Here is an example of how I used it in the set event processor. It reads the JSON file and then converts it into Java for my own needs but you can just replace java with JSON for your needs.
<munit:set-event doc:name="Set Event" doc:id="e7b1da19-f746-4964-a7ae-c23aedce5e6f" >
<munit:payload mediaType="application/java" value="#[output application/java --- readUrl('classpath://singleItemRequest.json','application/json')]"/>
</munit:set-event>
Here is the documentation for readUrl https://docs.mulesoft.com/mule-runtime/4.2/dw-core-functions-readurl
Hope that helps!
Follow this snippet (more specifically the munit-tools:then-return tag):
<munit-tools:mock-when doc:name="Mock GET /users" doc:id="89c8b7fb-1e94-446f-b9a0-ef7840333328" processor="http:request" >
<munit-tools:with-attributes >
<munit-tools:with-attribute attributeName="doc:name" whereValue="GET /users" />
</munit-tools:with-attributes>
<munit-tools:then-return>
<munit-tools:payload value="#[read(MunitTools::getResourceAsString('examples/responses/anypoint-get-users-response.json'), "application/json")]" />
</munit-tools:then-return>
</munit-tools:mock-when>
It mocks an HTTP request and returns a JSON object using the read() function.
I am getting this error:
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Emre\Desktop\PN1g1z.gif
And I really don't get what's wrong.
This is what throws the exception:
Media media = new Media(file.getAbsolutePath());
Media expects an URI as String in the constructor. So, instead of using File#getAbsolutePath(), you should be using File#toURI() instead.
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#toURI%28%29
From the Media#new JavaDoc (thanks #Andreas):
source - The URI of the source media.
Actually, it's a big problem where you put your server.
I already faced this problem before. I used Geronimo with the space in my direction D:\Common DevTool\Geronimo.
You have two ways to resolve:
Change to D:\Tool\Geronimo. It ran well.
Your directory is not correct: C:/Program Files. You should move your server to another place without the space in the name.
Upgrade the JSF version.
I had a similar problem but another exception: java.lang.IllegalArgumentException: Illegal character in opaque part at index 2: C:\Users\MyUser\project\src\main\packagex\file.csv
The project was created using java not by me. It tried to read a csv file. It worked ok on linux but crashed on windows 10.
My solution was change the absolute path to relative and switch reverse slash to "normal" slash. The url finally worked with this:
src/main/packagex/file.csv
It seems colon and reverse slash triggered the exception. That is the reason exception indicates index 2 because colon is located in second position at url
Illegal character in opaque part at index 2: C:\Users\MyUser...
Reference:
https://background.sysfactory.online/index.php/2022/09/23/solucion-java-lang-illegalargumentexception-illegal-character-in-opaque-part-at-index-2-cusersmyuser/
Alchemy API is used in my program for extracting keywords and relations from a URL.
When extracting these from the API calls I'm getting the error as follows,
'java.io.IOException: Error making API call: cannot-retrieve:downstream-http-error:404.
at com.alchemyapi.api.AlchemyAPI.doRequest(AlchemyAPI.java:960)
at com.alchemyapi.api.AlchemyAPI.GET(AlchemyAPI.java:914)
at com.alchemyapi.api.AlchemyAPI.URLGetRankedKeywords(AlchemyAPI.java:234)
at com.alchemyapi.api.AlchemyAPI.URLGetRankedKeywords(AlchemyAPI.java:224)
at innointel.feature1.Article.alchemyCall(Article.java:477)'
Then I found "http://venturebeat.com/2014/10/22/microsoft-and-ibm-partner-to-bring-enterprise-software-to-their-respective-cloud-platforms/" was the URL causing the error.I called the relation API simply putting the URL as follows ..
Document doc = alchemyObj.URLGetRelations("http://venturebeat.com/2014/10/22/microsoft-and-ibm-partner-to-bring-enterprise-software-to-their-respective-cloud-platforms/");
Well now, it contains no error . What is actually happening here ??
I found in some websites that "cannot-retrieve:downstream-http-error:404" is due to the invalid URL passed as argument.
Out Of 50 URL i tested 7 URL shows the error.Remaining works fine.And again when i extract the URL string placed it as a argument 7 URL works fine too..
(URL is parsed from an excel document using POI API)
Thanks in advance
As you said
'java.io.IOException: Error making API call: cannot-retrieve:downstream-http-error:404'
Exception is caused by the wrong URL argument in function call (ie.URLGetRankedKeywords())
Since the URL is parsed from the EXCEL Document some times the '\r' character might be present at the end of the cell.If present, this will make the URL invalid .
What you can do is , Remove all '\r' character from the URL before you pass it to API call.
ie ,
url = url.replaceAll("\r", "");
Document doc = alchemyObj.URLGetRelations(url);
This might work.it's worked for me.
I am doing an application where I have to read a URL from a webpage as a String[Its not the address of the page]. The URL that I will be reading contains query string, and I specifically need two queries from that URL. So I am using the Uri class available in Android. Now, the problem lies in the encoding/format of the URL and the query. One of the queries that I need is always an URL. Sometimes the query URL is %-encoded and sometimes not.
The URLs can be like the following :
Case 1 :
http://www.example.com/example/example.aspx?file=http%3A%2F%2FXX.XXX.XX.XXX%2FExample.file%3Ftoken%3D9dacfc85
Case 2 :
http://www.example.com/example/example.aspx?file=http://XX.XXX.XX.XXX/Example.file?token=9dacfc85
How do I get the correct Url contained in the file= query?
I am using the following [to accomplish the said work universally] :
Uri.decode(urlString.getQueryParameter("file"));
Is this the correct way to do it?
UPDATE
I have decided to first encode the whole URL regardless of its value and then get the query parameter. Theoretically, it should work.
If you are uncertain about the type of URL you would get then I would suggest you to decode every URL you get from the parameter. And when you need to use it then you can encode it.
As per my knowledge, you are doing it right.
I'm getting an exception saying Java URI Syntax Exception "java.io.IOException: java.net.URISyntaxException: Invalid % sequence: %wl in query at index 88:" when i try to connect from my android application.
It seems to be throwing the exception where in the URL it says "%wl" and following is the URL. is there a work around for this.
http://192.168.111.111:9000/RB/db.svc/upd?LinkId=184617ED1F21&IPs=fe80::1a46:17ff:feed:1f21%wlan0,192.168.1.127,&MNo=0771111111&sPin=000&Status=0
If you want to use % in your URL the first you need to do is to encode it.
So first you need to replace that % with %25 in your string ....1f21%wlan0... with .....1f21%25wlan0.... before connecting.
You can use the following code for encoding the URL in Java
String encodedUrl = java.net.URLEncoder.encode(<your_url>,"UTF-8");
Have a look at the below links for more information.
1.How to encode url in java
2.URL encoding character reference
UPDATE :
If you don't want to use URL encoder then you can try this out :
yourURL.replaceAll("%", "%25");
It is fine here to replace a single special character, but it would be a tedious task to do like this if you have many special characters that require proper URL encoding.