CURL in JAVA - Could not resolve host : POST - java

I have been successfully able to perform CURL commands from the CMD in my Windows PC by installing curl for Windows. Similarly, I have been able to get them working in my JAVA application by using ProcessBuilder and Process to create Operating System process. In particular, I need to execute the REST API end point commands used in KissFlow given here: https://support.kissflow.com/support/solutions/articles/179582-understanding-the-rest-api-end-points
Question: I have been able to able to execute the commands with the GET method like so.
ProcessBuilder pb = new ProcessBuilder("curl","-H","api_key:<XXXX>","-X","GET","http://<XXXX>.appspot.com/api/1/Employee/list/p1/50
However, when using the commands with POST like
ProcessBuilder pb = new ProcessBuilder("curl","-H","api_key:<XXXX>","-X ","POST","--data-urlencode","First Name=XXXX","http://<XXXX>.appspot.com/api/1/Employee/submit");, I get an error:
curl: (6) Could not resolve host: POST
with the input stream of the process returning
<html><title>Error 400 (Bad Request)!!1</title></html>
This in fact works perfectly when executed from CMD.
I have tried suggestions of all related questions here. Any help is appreciated. Thanks.
EDIT: Any alternate method from CURL to do the same will be acceptable as well.

According #GyroGearless's idea, try to use the sample code below to retrieve responses from URLs through methods GET and POST using Apache's HttpClient class. I think that with HttpClient you'll have much more "power" than with CURL.
You'll need commons-httpclient.jar and its dependencies: commons-codec and commons-logging. You'll find these jars at http://commons.apache.org/downloads/
(...)
String url = "http://<XXXX>.appspot.com/api/1/Employee/list/p1/50";
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(url);
client.executeMethod(get);
System.out.println(new String(get.getResponseBody()));
get.releaseConnection();
url = "http://<XXXX>.appspot.com/api/1/Employee/submit";
PostMethod post = new PostMethod(url);
post.addParameter("id", "10");
client.executeMethod(post);
System.out.println(new String(post.getResponseBody()));
post.releaseConnection();
(...)
As you can see, in the PostMethod part we're sending parameters within request. Maybe you don't need this....

You can also use Visual studio, we also created a post method where we reject requests in kissflow. Just letting you know that you can also do that in visual studio.

Related

How to authenticate with MS SharePoint to use its API with Java?

I'm struggling to authenticate with MS SharePoint to use it's API. I've been googling and playing around with that problem for a while but somehow I can't figure out a solution. The most promising solution so far is based on this answer.
The dependencies I'm using are:
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
This is my code:
public static void callRestEasyService() throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY),
new NTCredentials("user", "password", "https://workstation.de", "domain.de"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("https://adress/_api/web/lists");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
The code is quite self-explanatory I think, it is basically just what the linked answer suggests. My investigation showed that the best way to go for this problem are the NTCredentials. I also tried some other alternatives like the Basic Authentication but in all cases I receive:
HTTP/1.1 401 Unauthorized
I also tried using Samba JCIFS as an alternative NTLM engine.
Furthermore I'm a little bit scared that maybe the parameter for workstation (3rd parameter) or domain is filled in incorrectly by me. The documentation says:
workstation - The workstation the authentication request is originating from. Essentially, the computer name for this machine.
So I tried filling in the name of my machine but many examples on the web suggest that you use an URL that you are trying to authenticate with. This caused a little confusion for me but with none of the two options I could get it working.
Does anyone know why that is or has a possible solution or a workaround for that problem? Is it maybe possible that the SharePoint restricts the access via a programmed client? As far as I know it's at least not possible to disable the API from the SharePoint. Any ideas / thoughts to this?
I did not manage to get it going with the apache http client instead of this I tried running a cURL request from my java code to approach the SharePoint API, this worked fine instead. In case any one else has a similiar problem and wants to try a workaround this is my source code to run the cURL request:
ProcessBuilder pb = new ProcessBuilder(
"curl",
"https://mysharepoint/_api/web/lists/getbytitle('listName')/items",
"-v",
"--ntlm",
"--negotiate",
"-u",
"user:password"
);
Process p = pb.start();
InputStream is = p.getInputStream();
return createHashMapForStaff(convertStreamToString(is));
The return line (createHashMapForStaff(convertStreamToString(is)) is just adapting the retrieved .XML from the SharePoint to my needs. The InputStream from the Process p is basically what you need.

How to send parameters in a GET call with restlet?

Confirmed the following in Curl:
This is working fine-
curl -X GET 'http://remoteUrl.com:8080/public/private/request/data?begin=12&end=20'
This is not working-
curl -X GET http://remoteUrl.com:8080/public/private/request/data?begin=12&end=20
So the whole purpose is to use the way first curl command in a java code with restlet to send the paramters in a GET request but unfortunately my code is not working.
Code:
ClientResource clientResource = new
ClientResource("http://remoteUrl.com:8080/public/private/request/
data?begin=12&end=20");
Representation clientText = clientResource.get();
Error Log:
INFO: Unable to read a header
java.io.IOException: Parameter or extension has no name. Please check your value
at org.restlet.engine.header.HeaderReader.readNamedValue(HeaderReader.java:499)
at org.restlet.engine.header.CacheDirectiveReader.readValue(CacheDirectiveReader.java:65)
at org.restlet.engine.header.CacheDirectiveReader.readValue(CacheDirectiveReader.java:38)
at org.restlet.engine.header.HeaderReader.addValues(HeaderReader.java:282)
at org.restlet.engine.header.CacheDirectiveReader.addValues(CacheDirectiveReader.java:50)
at org.restlet.engine.header.HeaderUtils.copyResponseTransportHeaders(HeaderUtils.java:776)
at org.restlet.engine.adapter.ClientAdapter.readResponseHeaders(ClientAdapter.java:129)
at org.restlet.engine.adapter.ClientAdapter.updateResponse(ClientAdapter.java:191)
at org.restlet.engine.adapter.ClientAdapter.commit(ClientAdapter.java:105)
at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:119)
at org.restlet.Client.handle(Client.java:153)
From the exception you give in your question, it seems that the value of the header Cache-Control isn't correct within the response. Restlet isn't able to parse it.
I guess that the value of this header is an empty string in the response...
Could you give us in your question the exact content of the response (mainly headers)?
Hope it helps you,
Thierry

JAVA - Using httpclient to post a file to google apps via http proxy (squid) gets stuck when calling execute

Context
I have a desktop JAVA application I use to upload files (blobs) to a google app blobstore.
Everything works fine with a direct connection to the Internet but it doesn't when connecting through an HTTP proxy (Squid) with authentication.
I am using httpClient 4.2.3 and I don't get any error or response. It just gets stuck when calling httpClient.execute(post).
Code
I added these lines to handle the proxy authentication and it works well when using URL to get a page:
System.setProperty("http.proxyUser", username);
System.setProperty("http.proxyPassword", password);
I tried those as well:
Authenticator.setDefault(
new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username, password.toCharArray());
}
}
);
And from now on this is the same code that works when not using a proxy.
First of all I download a page where I get the url to use to post a file to the blobstore:
URL url = new URL("http://www.example.com/get-upload-url.jsp");
String urlWhereToPost=IOUtils.toString(url.openStream());
DefaultHttpClient client = new DefaultHttpClient ();
Here we prepare the multipart post:
HttpPost post
= new HttpPost( urlWhereToPost.trim() );
MultipartEntity entity
= new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart( "key"
, new FileBody(new File(jpgFilePath)
, "image/jpeg" )
);
post.setEntity((HttpEntity)entity);
And it is when calling execute that nothing happens (and it never get's to the next instruction):
HttpResponse execute = client.execute( post );
Tests
I have been trying several things but nothing worked:
In the beginning I thought the problem was using POST because GET works fine using URL()
but I tried using HttpClient to execute a GET and it gets stuck as well.
I used Wireshark to check the packets send to the proxy and I saw that when using URL() Wireshark recognizes the calls to the proxy as requests to execute a GET from the proxy. But when using httpClient it looks like the request is not well built because Wireshark shows a packet but doesn't recognize the inner request.
Then I tried building the POST using HttpURLConnection and it gets through the proxy and I get the answer from the server but it looks like I am not building it well because appengine doesn't find the file I send (but this would be another question...).
Conclusion
Anyone with the same problem? Any idea?
Your proxy settings are for the Java system classes. Apache HttpClient is supposed to be configured in a different way.
This link may help: Proxy authentication

Using HttpClient to send JSON query string in a URL to a web service (Java)

I have a web service that I built... what I am trying to do now is send a simple request that contains a json query string from a Tapestry web app to that web service. I searched around and most people say to use Apache HttpClient to achieve this. Along with HttpClient I am using URIBuilder.
The Json object that I am trying to send looks like this
{"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
*I realize the issues with the password being sent in plain text etc...
The url that works (tested by manually entering in a web browser and this web service already services an Android client and an iOS client) looks like this
http:// ##.##.###.##/createuser?json={"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203-555-5555"},"password":"dead"}
Here is the HttpClient code that I have mashed together from google'ing around trying to figure out why this wont work. Essentially what I am trying to do is create a URI with URIBuilder and then construct an HttpPost or HttpGet object with the newly built URI. But something is going wrong in the URIBuilding process. When I debug, an exception gets thrown when I try to set all the aspects of the URI.
Object onSuccess() throws ClientProtocolException, IOException, URISyntaxException{
// json = {"user":{"userEmail":"jdoe#gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
String json = user.toJson();
URIBuilder builder = new URIBuilder();
// Error gets thrown when I step over the next line
builder.setScheme("http").setHost("##.###.##.###").setPort(8080).setPath("createuser").setQuery("json=" +json);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
DefaultHttpClient httpClient = new DefaultHttpClient();
String tmp = request.getURI().toString();
HttpResponse response = httpClient.execute(request);
index.setResponse(EntityUtils.toString(response.getEntity()));
return index;
The error that comes back when I step over the line that I commented in the code is
[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;
java.lang.NoSuchMethodError:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;
I have tried a lot of other combinations of methods and objects to get this request to send off to the server correctly and nothing seems to work. Hopefully I am overlooking something relatively simple.
Thanks in advance for any guidance you can provide.
You most likely have the wrong version or two versions of the apache httpcomponents on your classpath. If you are running Tapestry it will print out all packages on the classpath on the error page. Investigate there, find which httpcomponents is loaded, figure out where it comes from and fix it.
If this does not work, you should share some of your runtime environment with us. Which servlet engine, running from which IDE or are you running from the command line. Are you using Maven? If so share your pom. Etc.

What's the easiest way to get the result of an HTTP GET request in using URL in JRuby

I'm attempting to build a Tropo Ruby application and I need to retrieve the result of an HTTPS GET. The Tropo platform doesn't have the httpclient Ruby gem so I can't use that. The Ruby engine used is JRuby so a suggestion has been to make use of the Java URL class to do the request. I've played around with it a little bit and I seem to be able to create the URL object ok but am now struggling with how to get the results of executing the request. How do I do it?
javaURL = java.net.URL.new svcURL
transferResult = javaURL.getContent()
I am not sure what getContent() does exactly, but you can use openStream() which gives you an InputStream to read from.
javaURL = java.net.URL.new svcURL
transfer = javaURL.openStream()
// read from stream
transfer.close()

Categories

Resources