How do I write the following APIGEE related curl command in Java
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
https://{org-name}-test.apigee.net/oauth/client_credential/accesstoken?
grant_type=client_credentials \
-d 'client_id={consumer-key}&client_secret={consumer-secret}'
Using curl is not allowed in our company so want to execute the command from Java. Any help would be appreciated.
Note - To some extent this URL was helpful How do i do the following curl command in Java
Related
Currently we are working on a requirement, where we need to create sub directories in azure data bricks using java.
From Databricks UI, we are able to create subdirectory. Whether we can create subdirectory from Java code.
folders created from databricks UI
file inside created folder
Using curl command, we are able to create subdirectory from terminal. But, when we use ProcessBuilder in Java code it is not creating subdirectory.
Curl command for reference :
curl -n -X POST -H 'Authorization: Bearer ' -H 'Content-Type: application/json' -d '{ "path":"/Users/.com/testfolder4" }' https://*.azuredatabricks.net/api/2.0/workspace/mkdirs
I am trying to setup a sample OAuth2.0 server using this guide.
https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation
Following the instructions from the readme, I start the server with:
$ cd vanilla
$ mvn package
$ java -jar target/*.jar
However, when I run
curl -H "Accept: application/json"
my-client-with-secret:secret#localhost:8080/oauth/token -d
grant_type=client_credentials{... "access_token":
"b561ff06-4259-466e-92d8-781db1a51901", ...}
I get
Could not resolve host: access_token curl: (6) Could not resolve host:
b561ff06-4259-466e-92d8-781db1a51901,
The error message is a big hint. You need to quote your arguments.
After reading on github, the command you must issue is
curl -H "Accept: application/json" my-client-with-secret:secret#localhost:8080/oauth/token -d grant_type=client_credentials
(usually when the line starts with $ it's the command the next one(s) is/are the result)
How does one post a message in an ActiveMQ topic using curl? I tried:
curl -XPOST -d "body=message" http://admin:admin#localhost:8161/api/message?destination=queue://orders.input
but the response is unauthorized user 401.
Per the ActiveMQ documentation both of the following worked for me:
curl -XPOST -u admin:admin -d "body=message" http://localhost:8161/api/message/orders.input?type=queue
curl -XPOST -d "body=message with alt syntax" http://admin:admin#localhost:8161/api/message?destination=queue://orders.input
Are you able to log into the web interface and view the queues using the credentials admin/admin? Try to view http://localhost:8161/admin/queues.jsp to confirm.
I am having trouble using the session-based speech recognition interface. Specifically, I am trying to split a longer audio stream into multiple chunks, upload them one at a time, and receive the complete parsed text at the end (as opposed to streaming the chunked audio from a single source).
IBM Watson's offers both stateless and stateful interfaces to speech recognition. The more common stateless protocol accepts a (chunked) audio stream and returns the parsed content on completion. The session-based approach allows the client to establish a persistent session, upload the audio as multiple chunks using multi-part, and query for the results, which can be very useful for processing long streams or processing microphone input.
I was able to find some tutorials and discussions but none of the examples seem to work (likely out of date, as the interface is evolving rapidly).
Here's a representative sample. The following POST will create a session:
curl -X POST -u "user:password" -H "Content-Type: application/json" \
https://stream.watsonplatform.net/speech-to-text/api/v1/sessions -verbose -d ""
Then, the next one should submit a portion of the audio data to recognize service, using the endpoints provided by the previous command:
curl -k -X POST -u "user:password" \
-H "content-type: audio/flac" --data-binary #temp.2.flac -H "Transfer-encoding: chunked" \
--cookie "SESSIONID=65097570295a0eccd15fd6dba326487416634371; Secure" \
https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/65097570295a0eccd15fd6dba3264874/recognize -verbose
Finally, this command should return the results:
curl -k -X GET -u "user:password" \
--cookie "SESSIONID=65097570295a0eccd15fd6dba326487416634371; Secure" \
https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/65097570295a0eccd15fd6dba3264874/observe_result -verbose
The first command completes without any issues, returning HTTP 201 Created status, as well as reasonably looking endpoints, which are used (together with the SESSIONID cookie) for subsequent calls.:
"recognize": "https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/65097570295a0eccd15fd6dba3264874/recognize",
"recognizeWS": "wss://stream.watsonplatform.net/speech-to-text/api/v1/sessions/65097570295a0eccd15fd6dba3264874/recognize",
"observe_result": "https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/65097570295a0eccd15fd6dba3264874/observe_result",
"session_id": "65097570295a0eccd15fd6dba3264874",
"new_session_uri": "https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/65097570295a0eccd15fd6dba3264874"
However, both the 2nd and 3rd command fail with HTTP code of 404 "Session does not exist." error.
Any curl or Java pointers or examples would be greatly appreciated.
Robert,
I was just made aware of this post; sorry for the delay. I'm not sure how you're issuing the commands, but the issue may be that the session timed out before the subsequent calls. If the default 30-second session timeout expires before the subsequent calls, the service returns a 404 with the indicated message. It could also be an issue with how you're providing the cookie, as the previous user indicates. But I've experienced the session timeout issue, which could also be the culprit.
I wrote a Gist that uses curl commands to recognize a PCM file. In your case you just need to change the audio format and point to your file.
See https://gist.github.com/germanattanasio/ae26dc0144f229ad913a
When dealing with cookies it's always good to save them in a file and then use that file in the subsequent request.
For example
curl -X POST -u "user:password" -H "Content-Type: application/json" \
https://stream.watsonplatform.net/speech-to-text/api/v1/sessions \
-verbose -d ""
could be writen as:
curl -X POST -b cookies.txt -c cookies.txt -u $USERNAME:$PASSWORD \
"https://stream.watsonplatform.net/speech-to-text/api/v1/sessions" \
-d ""
The result will be the same and cookies.txt will have the SESSIONID.
Then you can use:
curl -X POST -b cookies.txt -c cookies.txt -u $USERNAME:$PASSWORD \
"https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/$SESSION_ID/recognize?continuous=true" \
--header "Content-Type: audio/flac" --header "Transfer-Encoding: chunked" \
--data-binary #temp.2.flac
Make sure $SESSION_ID is updated with the value you get in the first curl command.
I have a curl syntax in .sh file. I need to run the curl sytnax or curl command in Java replicating the same syntax, but I am facing problem in replicating the same.
$AUTH_OPTION="--basic -u testuser:testpwd"
$HTTP_METHOD=POST
$FILE_OPTION="-d #$INPUT_FILE"
$CONTENT_TYPE="application/xml"
$ACCEPT_TYPE="application/xml"
echo curl -o response.txt -w %{http_code} -k -v $AUTH_OPTION -X $HTTP_METHOD $FILE_OPTION -H \"Content-Type: $CONTENT_TYPE\" -H \"Accept: $ACCEPT_TYPE\"
I have the corresponding Java code as:
StringBuffer curlCmd=new StringBuffer();
curlCmd.append("curl -o response.txt");
curlCmd.append(WHITE_SPACE);
curlCmd.append("-w %{http_code}");
curlCmd.append("-k -v -u testuser:testpwd");
curlCmd.append(WHITE_SPACE);
curlCmd.append("-X POST");
curlCmd.append(WHITE_SPACE);
curlCmd.append("-d #/test/xyz/xml" );
curlCmd.append(WHITE_SPACE);
curlCmd.append("-H"+"Content-type: application/xml");
curlCmd.append(WHITE_SPACE);
curlCmd.append("-H"+" Accept: application/xml");
curlCmd.append(WHITE_SPACE);
This does not seems to work: its not simulating the same behaviour of .sh curl syntax. Can any one help me to sort out this issue?
output
curl -o response.txt -w %{http_code} -k -v -u testuser:testpwd -X POST -d #/path/xyz.xml -H "Content-Type: application/xml" -H "Accept: application/xml"
the problem is xml is not getting accessed properly
I think there are a few possible problems, but one that catches my eye is that you are missing quotes around Content-Type: $CONTENT_TYPE and Accept: $ACCEPT_TYPE, for example:
"-H \"Content-type: application/xml\""
A second error is you have written -d #/test/xyz/xml but it should be:
-d #/test/xyz.xml
If it still doesn't work, can you post the output of both the sh script and your StringBuffer so we can more easily see where the differences are?
If you're using environment variables then you need to make sure that:
they are exported
you execute curl via a shell (e.g. /bin/bash)
The exporting means that the variables are exposed to child processes. The shell execution will expand these prior to calling your executable.
So your invocation will look like:
sh curl ....
It would help to see how you're invoking Process.exec(). One common gotcha is that you need to consume the stdout/stderr of the process concurrently, otherwise your sh/curl process may block waiting for your parent process to consume the output. See here for more details.
You should probably replace %{http_code} with something else on line 4 of the Java code. Environment variables will not be interpolated by Java.
Also, take a look at the Runtime#exec method. This lets you execute commands without having to worry about escaping quotes and such.
It's probably a good idea to make sure that your command runs without problems (such as the server not accepting the posted content) before trying to debug the invocation from java. It's far easier to deal with one problem at a time.