Skip to content

Linux cURL code to use variable API key & message #2

@tel77

Description

@tel77

Hi xurei,

I'm trying to write a bash script to send a message with nimrod via cURL. It works when I hard code the API key and message into the cURL command, but I can't get it to work when I try to use them in variables. I've tried changing the quotes (') to (") so the variable is not taken literally, but it still fails. For example, this code:

API_KEY=99x9999-x9x9-9x9x-9999999999   # Changed for privacy
MSG="This is a test"

curl -X POST -H "Content-Type: application/json" -d "{
        'api_key': '$API_KEY',
        'message': '$MSG'
}" "https://www.nimrod-messenger.io/api/v1/message"

Gives this error:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token &#39; in JSON at position 3<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at parse (/var/www/nimrod/node_modules/body-parser/lib/types/json.js:88:17)<br> &nbsp; &nbsp;at /var/www/nimrod/node_modules/body-parser/lib/read.js:116:18<br> &nbsp; &nbsp;at invokeCallback (/var/www/nimrod/node_modules/raw-body/index.js:262:16)<br> &nbsp; &nbsp;at done (/var/www/nimrod/node_modules/raw-body/index.js:251:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/var/www/nimrod/node_modules/raw-body/index.js:307:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (node:events:406:35)<br> &nbsp; &nbsp;at IncomingMessage.emit (node:domain:475:12)<br> &nbsp; &nbsp;at endReadableNT (node:internal/streams/readable:1343:12)<br> &nbsp; &nbsp;at processTicksAndRejections (node:internal/process/task_queues:83:21)</pre>
</body>
</html>

Any ideas how to resolve this?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions