tags:

views:

351

answers:

3

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

A: 

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?

Mark Byers
i have used the syntax as u said but still the problem persist.i am getting badrequest error ,i am trying to run this program in linux using runtime api
sarah
I think you should instead of executing the command, try displaying it to System.out or view it in a debugger. It is probably quite obvious what is wrong if you do that. If not, post the string you get here and someone will quickly show you what to fix. We can't really help you if you just tell us "It doesn't work".
Mark Byers
i am exactly not getting the proper syntax of curl to be used in java..,the output i am getting as the following syntaxcurl -o response.txt -w %{http_code} -k -v -X POST -d @/path/xyz.xml -H "Content-Type: application/xml" -H "Accept: application/xml"
sarah
this is the system.out of stringbuffer value
sarah
Post this and also the output of the echo statement in your question where everyone will see it.
Mark Byers
I've updated my answer with a second correction to your Java. PS: the formatting of your output makes it very difficult to read. Please put the output of the curl script all on one line using code formatting, then put the output of your StringBuffer.toString() on the next line, using the same formatting. The error should become immediately obvious.
Mark Byers
A: 

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.

jackrabbit
I have using the same runtime command to execute ,but the output is bad request
sarah
Sarah, you need to replace `http_code` since it's an environment variable! Or use `System.getenv("http_code")`.
Geo
okay but what should be the value of http_code?
sarah
no, it's NOT an environment variable, it's a way to ask curl to print that information after it's done
Daniel Stenberg
I did not know about the %{...} thing - useful to know. Removing that bit from my answer.
jackrabbit
A: 

If you're using environment variables then you need to make sure that:

  1. they are exported
  2. 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.

Brian Agnew
okay ,right now i am not using any environment variables...,do we need to execute curl using this syntaxsh curl...curl itself is a command ,why we need sh ?
sarah
If you're not using environment variables, then you don't need the /bin/bash. The shell will expand any environment variables that you reference on the command line, so if you're not using any then executing curl alone will be fine. My comments re. concurrency remain, however!
Brian Agnew