0

I'm trying to run a curl command such as:

curl -Sks -XPOST https://localhost:9999/some/local/service -d 'some text arguments'

The above works fine. However, when I place

'some text arguments'

in a file and call the command as:

curl -Sks -XPOST https://localhost:9999/some/local/service -d `cat file_with_args`

I get many exceptions from the service. Does anyone know why this is?

Thanks!

1 Answer 1

3

If the file contains 'some text arguments', then your command is equivalent to this:

curl -Sks -XPOST https://localhost:9999/some/local/service -d \'some text arguments\'

— that is, it's passing 'some, text, and arguments' as three separate arguments to curl.

Instead, you should put just some text arguments in the file (no single-quotes), and run this command:

curl -Sks -XPOST https://localhost:9999/some/local/service -d "`cat file_with_args`"

(wrapping the `cat file_with_args` part in double-quotes so that the resulting some text arguments doesn't get split into separate arguments).

Incidentally, I recommend writing $(...) rather than `...`, because it's more robust in general (though in your specific command it doesn't make a difference).

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, wonderful! Thanks, this worked! How did you know this? And how would I go about debugging this? The error message was just HTTP 500. How would one even guess that the input is being split into multiple arguments?
@newbie To debug this, work from in inside out. Compare cat file_with_args => some text arguments versus 'some text arguments'.
@newbie: One helpful approach is to preface the command with printf "<%s>\n", e.g. printf "<%s>\n" curl -Sks -XPOST https://localhost:9999/some/local/service -d `cat file_with_args` . That will print one argument per line, wrapped in <...>, so you can see what arguments you're actually passing to curl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.