3

I'm using curl command inside logstash exec plugin to post a message to a stride group. The plugin documentation stated that they were using ruby system method to execute the command, so I'm trying to run it in my ruby IRB.

Escaping the double quotes with backslash character is giving error The request body cannot be parsed as valid JSON. Here is the full error

irb(main):050:0' --inf-ruby-2f3827a9-23243-13517-726000--
curl: (6) Couldn't resolve host 'first'
curl: (3) [globbing] unmatched close brace/bracket in column 9
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 165 0 87 100 78 60 54 0:00:01 0:00:01 --:--:-- 60
{
"statusCode": 400,
"message": "The request body cannot be parsed as valid JSON"
}=> true

I tried swapping double quotes with single quotes and using full double quotes everywhere. Nothing is working.

system("curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer blah-blah' -d '{\"body\":{\"version\":1,\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"My first message!\"}]}]}}' --url 'https://api.atlassian.com/site/blah-blah/conversation/blah-blah/message'")

Is there any way to make this work?

EDIT: I have tried running the cURL command in the terminal and it is working fine.

vishless
  • 829
  • 1
  • 5
  • 28
  • Does your `cURL` command work from the terminal? – Tom Lord Apr 09 '18 at 09:33
  • 1
    Yes it does work from the terminal – vishless Apr 09 '18 at 09:43
  • I cannot reproduce your problem. I see no reason why your code won't work. Can you provide a [MCVE]? Perhaps a cURL request to a public site, so I don't get a 401 error? My best guess is that _something else_ is going wrong - e.g. the error is actually from a different line of code?! – Tom Lord Apr 09 '18 at 10:04
  • 1
    The line that I posted is the only line in the ruby script. – vishless Apr 09 '18 at 11:07
  • The error doesn't come from Ruby, it comes from the remote host. A simple test to verify your command line formatting is to replace `curl` with `echo`. But in any case it seems the remote host cannot parse your request. First get it working on the command line, then try and convert it to Ruby. – Casper Apr 09 '18 at 12:25
  • This is not answering your question, but I would advice you to use a gem like `httparty` to make such requests from ruby directly. Your approach requires `curl` to be installed, which may not be always the case for every system where your app is running. Also, and more importantly, you can pass in a hash as body. Any character conversions is handled by the gem. This makes the usage much simpler and less error prone. – mbuechmann Apr 09 '18 at 15:00
  • @Casper The error does come from the remote host since the string is not properly formatted within ruby system method. I already tried echoing that's how I understood that its an error with the string format. It is working on the command line as I said in the question. – vishless Apr 10 '18 at 07:10
  • @mbuechmann Thanks for the advice, I cannot use ruby gems since this command has to be run inside a logstash plugin called `exec` which uses ruby system method internally to run the commands given to it, hence I'm using ruby to debug the issue. – vishless Apr 10 '18 at 07:12
  • 1
    i think the issue is with your quotes check this https://superuser.com/questions/1168854/running-curl-command-inside-a-rails-application – Ali Akbar Apr 10 '18 at 07:20

1 Answers1

2

Besides the following answers on StackOverflow, NetHTTP has notoriously poor documentation but can be used to post what you're interested in.

W4t3randWind
  • 631
  • 1
  • 7
  • 16