3

I am running out of ideas to have this to work in my gitlab-ci.yml.

I have a variable called TARGET, and I want to successfully inject its value inside the json payload of my http POST request in form of curl bash command.

job:
  stage: deploy
  script:
    - curl -k --request POST ${URL} --header "Authorization:Basic ${TOKEN}" --header 'Content-Type:application/json' --data "{"extra_vars":{"target":${TARGET}}}"
  variables:
    TARGET: host1

the pipeline output keeps complaining with error message: {"detail":"JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)\nPossible cause: trailing comma."}

I also tried escaping some special characters with \ as below, but not working either:

curl -k --request POST ${URL} --header "Authorization:Basic ${TOKEN}" --header 'Content-Type:application/json' --data "\{"extra_vars":\{"target":${TARGET}\}\}"

your helps are very appreciated.

0

1 Answer 1

2

There are two issues here. You are missing double quotes around the value for target inside the JSON since TARGET is a string value.

You are also wrapping your data argument with double quotes hence you should not use them inside the payload without either escaping them or switching to single quotes.

Keeping that in mind the payload may look something like this:

'{"extra_vars":{"target":"'${TARGET}'"}}'

Here's a quick tip on how to test the output using httpbin.org:

TARGET=host1 && curl -v https://httpbin.org/post -H "Content-Type: application/json" -d '{"extra_vars":{"target":"'${TARGET}'"}}'

Which will respond with the sent payload (which is ofcourse escaped as it is also wrapped inside JSON:

"data": "{\"extra_vars\":{\"target\":\"host1\"}}",
Sign up to request clarification or add additional context in comments.

Comments

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.