1

I have this python script

users=['mark','john','steve']

text=''

for user in users:
    text+=str(user + " ")

print(text)

I want to output that string "text" into a curl terminal command.

I tried:

curl -d "@python-scirpt.py" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

and

curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": 'python /home/scripts/python-script.py'}' https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

or without the quotations in the text option

Everything returns this error

{"id":"Unable to parse incoming data","message":"Unable to parse incoming data","detailed_error":"","request_id":"fpnmfds8zifziyc85oe5eyf3pa","status_code":400}

How to approach this ? Any help is appreciated thank you.

another approach would be to curl inside python but would need help in that too. Thank you

5
  • 2
    Python have requests module to make HTTP requests. Commented Apr 6, 2018 at 11:52
  • 1
    In the python script, instead of running all that you can just do ' '.join(users) for creating the string out of the list Commented Apr 6, 2018 at 11:57
  • Thank you @MartínGómez I am very new to python so had no knowledge of this yet, very helpful. Commented Apr 6, 2018 at 12:07
  • @ABDULNIYASPM would you care to guide a bit through it or reference a link or anything Commented Apr 6, 2018 at 12:24
  • 2
    The requests docs are a very good resource Commented Apr 6, 2018 at 12:30

1 Answer 1

1

Use command substitution (i.e. $(...)) to make the shell run the python code first.

So

curl -d "$(python-scirpt.py)" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e
Sign up to request clarification or add additional context in comments.

4 Comments

/home/scripts/python-script.py: line 12: syntax error near unexpected token ` (' /home/scripts/python-script.py: line 12: `text=' '.join(users)' curl: (51) SSL: certificate subject name 'chat2.temple.lan' does not match target host name '10.10.10.6' weird that it says chat2.temple.lan because it is chat.temple.lan
The first error is to do with your Python script. Run that on its own until you get it working. Second error is probably because you're trying to use https for an IP address, but the site seems to have a cert only linked to its domain name (i.e. chat2.temple.lan). If it's on a local trusted network you can use http instead.
Also when I run this for instance curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": "Testing"}' 10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e It works
If you're sending data as JSON you should probably use json.dumps to ensure it's formatted correctly, see docs.python.org/3/library/json.html

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.