Jeffery Tang
Jeffery Tang

Reputation: 393

Formatting curl command

In Windows Git Bash, this curl command works:

curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' \
  -X 'POST' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'client_id=6BfkncD9ypGMiHkjrfka5ydqrG4GLx1z&client_secret=EC5i7QG61Qg8jDmZ&grant_type=client_credentials&scope=data:read'

But when I try to format the contents in -d, it doesn't work:

curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' \
  -X 'POST' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'client_id=6BfkncD9ypGMiHkjrfka5ydqrG4GLx1z&
    client_secret=EC5i7QG61Qg8jDmZ&
    grant_type=client_credentials&
    scope=data:read
  '

Anyway I can format it and it works?

Upvotes: 1

Views: 5987

Answers (1)

VonC
VonC

Reputation: 1325155

The -d/--data/--data-ascii option of curl sends a data which is expected to be in one line in case of URL paraleters (as opposed to JSON, as seen for instance here, where the data can be on multiple lines).

That means you need to build your string first in a variable, then use that variable in curl.

vonc@vclp MINGW64 /c/test
$ data=$(printf '%s' '
    client_id=6BfkncD9ypGMiHkjrfka5ydqrG4GLx1z&
    client_secret=EC5i7QG61Qg8jDmZ&
    grant_type=client_credentials&
    scope=data:read
' | sed ':a; N; $!ba; s/\n\s*//g')

curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' \
  -X 'POST' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d "${data}"

So two steps in this case.

Upvotes: 3

Related Questions