websocat icon indicating copy to clipboard operation
websocat copied to clipboard

websocat send json arrays

Open beckietech opened this issue 4 years ago • 7 comments

Hello, I tried using this link: https://stackoverflow.com/questions/48912184/wscat-commands-from-script-how-to-pass/48914019

however, it didn't work for me. I checked the readme and wondering if there's any solution maybe using readfile? but I can't find example file of hello.json. Please kindly help, thanks!

beckietech avatar Mar 07 '21 13:03 beckietech

Do you want to just

  1. Connect to a Websocket server;
  2. Send single WebSocket message that contains a single JSON array;
  3. Disconnect from the server (without waiting for any reply message or sending additional messages)?

If yes, the readfile: specifier (or maybe just using redirection) may be appropriate, combined with a large buffer size (larger than maximum possible JSON array byte size).

Note that by default Websocat treats one line of input text as one WebSocket message. So your JSON arrays may just work if you use minified JSON representation, not indented multiline one.

vi avatar Mar 07 '21 20:03 vi

Thank you!Vi, yes, I would like to do that, but I can't understand:

  1. (or maybe just using redirection): What does it mean? can you provide an example of that?
  2. So your JSON arrays may just work if you use minified JSON representation: can you also provide an illustration? Sorry Im newbie to this. Thanks!!

beckietech avatar Mar 08 '21 00:03 beckietech

I don't understand what you are trying to do and what error do you get.

A minified JSON representation is like this:

[{"a":3,"b":null},{"a":null,"b":3.4}]

while human-readable is like this:

[
   {
      "a":  3,
      "b":  null
   },
   {
      "a":  null,
      "b":  3.4
   }
]

If you try to input the latter into Websocat, you typically get instead of one well-formed JSON message the multiple message like this:

  • [
  • {
  • "a": 3,
  • "b": null
  • },

(and so on)

This would cause server to reject those messages as invalid JSON arrays.

If you do indeed type (or otherwise provide) your message as one line, it should work. If not, describe it as a bug, with a proper "steps to reproduce" / "expected" / "actual" fields.

vi avatar Mar 08 '21 01:03 vi

Thank you, I did it and got following warning: [WARN websocat::line_peer] Sending possibly incomplete line. however, I didn't se expected outcome. maybe something wrong.

beckietech avatar Mar 08 '21 03:03 beckietech

Hi, Vi, When I return to stackoverflow, I saw you replied the following: @CharlesDuffy, The new websocat is released and exec: now has --exec-args option that makes all remaining options go into exec-ed program arguments.

I'd like to know more about how to do it, could you please explain to me? Thanks in adv.

beckietech avatar Mar 08 '21 03:03 beckietech

You need to describe how you want to use Websocat (and Websockets in general) in more detail for me to help.

Sharing command line terminal sessions or scripts would also be helpful.

vi avatar Mar 08 '21 10:03 vi

What I wanted to do is to pass json in command arrays or json files and pass them into ws:// connection, as stackoverflow told, I got the following error: bash: run_init: command not found

and my script is called as following: export -f run_init ./websocat --text $url sh-c:'exec bash -c run_init'

Any suggestions?

beckietech avatar Mar 08 '21 11:03 beckietech