Home > Back-end >  How to make curl/jq treat a string with spaces characters as single string?
How to make curl/jq treat a string with spaces characters as single string?

Time:02-03

How do we make curl/jq treat a string with spaces characters as a single string?

I have JSON data in a variable

authArray=[
  {
    "name": "testtoken1",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization: Bearer {{@CmdCache | curl -s -d '{username: [email protected], password: admin5665}' -H Content-Type:application/json -H Accept:application/json -X POST https://dev.qtech.ai/login | jq --raw-output .token}}",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": false,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Invalid_Auth",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization:BearereyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": true,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Invalid_Auth_Empty",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization:Bearer",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": true,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Invalid_Auth_SQL",
    "authType": "Token",
    "username": null,
    "password": "PASSWORD-MASKED",
    "header_1": "Authorization:Bearer{{@Injection}}",
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": true,
    "lastTestedOn": null,
    "passwordMasked": true
  },
  {
    "name": "Default",
    "authType": "Basic",
    "username": "[email protected]",
    "password": "PASSWORD-MASKED",
    "header_1": null,
    "header_2": null,
    "header_3": null,
    "clientId": null,
    "clientSecret": null,
    "id": null,
    "accessTokenUri": null,
    "authorizationScheme": null,
    "clientAuthenticationScheme": null,
    "tokenName": null,
    "scope": null,
    "grantType": null,
    "preEstablishedRedirectUri": null,
    "useCurrentUri": null,
    "userAuthorizationUri": null,
    "inactive": false,
    "invalid": false,
    "lastTestedOn": null,
    "passwordMasked": true
  }
]

I am trying to update 1st object named testtoken1's 'header_1' field in that array with the below dynamically updated string with code given as well

auth='"Authorization: Bearer {{@CmdCache | curl -s -d '{"username":"[email protected]","password":"Welcome123"}' -H '"Content-Type: application/json"' -H '"Accept: application/json"' -X POST '"https://dev.qtech.ai/login"' | jq --raw-output '".token"' }}"'

mAuth=$(echo $authArray | jq 'map(select(.name == "testtoken1") |= (.header_1 = "'${auth}'" ))' | jq -c . )

When I execute the above code, I'm getting below jq error message

jq: Unknown option -d
Use jq --help for help with command-line options,
or see the jq manpage, or online docs  at https://stedolan.github.io/jq

It's throwing error because of space characters in that auth variable, if I remove those spaces characters from that variable it's working perfectly.

Above commands output needs to used with below code eventually.

udto=$(echo $(_jq '.') | jq '.auths = '${mAuth}'')
curl -s --location --request PUT "${HOST}/api/v1/projects/$PROJECT_ID/env/$eId" --header "Accept: application/json" --header "Content-Type: application/json" --header "Authorization: Bearer "$token"" -d "$udto"

But our use case is as such, we want to put auth variable as it is so that we don't have to make/add space characters back again from the UI of the app and when we run that command from UI by pressing Run button which we will execute that curl structure/syntax command in the background.

So, how can we make/pass that variable as it is along with space characters using curl, jq, and bash shell scripting?

CodePudding user response:

It's because you are not quoting your variable when expanding:

jq 'map(select(.name == "testtoken1") |= (.header_1 = "'${auth}'" ))'

should be

jq 'map(select(.name == "testtoken1") |= (.header_1 = "'"${auth}"'" ))'

But you shouldn't be interpolating the jq program/filter with shell variables in the first place. Define a jq variable instead and don't worry about messing up your jq filter:

jq --arg auth "$auth" 'map(select(.name == "testtoken1") |= (.header_1 = $auth))'
  • Related