With curl, how can I pass multiple command line parameters in a POST?

1

I have the following controller:

public IActionResult Post([FromQuery] int Width, [FromQuery] int Height, [FromForm] IFormFile Image)

With Insomnia / Postman, I can do a post command and pass the width / height parameters in the url.

I am trying to do the same with CURL but the second parameter is not seen. In this case, height will be 0

curl -F image=@photo.jpg test/thumbnail?width=320&height=240

In that case, width will be 0

curl -F image=@photo.jpg test/thumbnail?height=240&width=320

What am I missing?

Thomas

Posted 2018-05-08T22:00:39.000

Reputation: 153

what OS/utility set are you working on? i'd start by reading the man page or equivalent for your implementation/distribution/whatever and check the --data or -d option – ivanivan – 2018-05-09T00:03:36.500

I'm on Mac; I tried -d "width=320" -d "height=240" but Curl told me it wasn't compatible with the multi-part post – Thomas – 2018-05-09T00:04:35.817

Try them together - -d "width=320&height=240" – ivanivan – 2018-05-09T00:19:48.773

Answers

1

The most likely problem is that & is a special character in your macOS shell (the command-line interpreter); if left unquoted, it acts as a command separator and runs the preceding command in background. The rest (height=240) is interpreted as a second command by macOS.

So the parameter with & must be quoted, or the & itself escaped with a backslash:

"test/thumbnail?height=240&width=320"
test/thumbnail?height=240\&width=320
test/thumbnail?height=240"&"width=320
test/thumbnail?'height=240&width=320'
etc.

(? is also special in that it's a wildcard, but that's only a problem if it happens to match a real filename on the local system.)

The comments suggesting -d aren't correct. For one, they send parameters as POST payload – but the controller wants query-string (GET) parameters, which isn't the same thing at all. For another, the request can only use one format at a time – you cannot mix -F and -d in the same request.

user1686

Posted 2018-05-08T22:00:39.000

Reputation: 283 655

works with escaping! – Thomas – 2018-05-09T17:20:36.927