Skip to main content

Websocket plugin for httpie

Project description

httpie-websockets

PyPI version License Static Badge PyPI - Downloads Pepy Total Downlods

Home: https://github.com/belingud/httpie-websockets

httpie-websockets is an HTTPie CLI plugin that adds WebSocket support to the HTTPie command line.

Features

  • WebSocket Support: Seamlessly connect to WebSocket servers using the familiar HTTPie command line interface.
  • Bidirectional Communication: Send and receive messages in real-time.
  • Secure Connections: Supports both ws:// and wss:// protocols.
  • Easy Integration: Simple installation and usage within the HTTPie environment.

Install

You can install by httpie plugins command:

httpie plugins install httpie-websockets

or use pip in the same environment with httpie

pip install httpie-websockets

If your httpie is installed with pipx, you also can use pipx to install httpie-websockets, If you cannot use it properly after installation。

Suppose your httpie environment is named httpie.

# Replace httpie with your httpie venv name
pipx inject httpie httpie-websockets  # will auto upgrade version
# or
pipx runpip httpie install -U httpie-websockets

Usage

After install this plugin, just pass websocket url to http command.

http ws://localhost:8000/ws

This allows HTTPie to interact with WebSocket servers directly from the command line.

Example:

$ http wss://echo.websocket.org
> wss://echo.websocket.org
Type a message and press enter to send it
Press Ctrl+C to close the connection

When you press CTRL+C, connection will disconnect and httpie will get handshake response headers and websocket connection info with close code and close message like below:

^C
Oops! Disconnecting. Need to force quit? Press again!
HTTP/1.1 200
connection: Upgrade
date: Thu, 15 Aug 2024 13:24:10 GMT
fly-request-id: 01J5B3BHGV549MMJQ474SF7J60-sin
sec-websocket-accept: MV41qn7qZQP3IXsTzYS5eDRe2tE=
server: Fly/ddfe15ec (2024-08-14)
upgrade: websocket
via: 1.1 fly.io

Websocket connection info:
Close Code: 1006
Close Msg: KeyboardInterrupt

Debug Log

You can set HTTPIE_WS_LOG_LEVEL to DEBUG to see httpie_websocket debug log for more information.

On linux and Mac:

export HTTPIE_WS_LOG_LEVEL=DEBUG

Or

HTTPIE_WS_LOG_LEVEL=DEBUG http wss://echo.websocket.org

On Windows:

set HTTPIE_WS_LOG_LEVEL=DEBUG

Or

# Powershell
$env:HTTPIE_WS_LOG_LEVEL="DEBUG"; http wss://echo.websocket.org
# Cmd
cmd /C "set HTTPIE_WS_LOG_LEVEL=DEBUG &&; http wss://echo.websocket.org"

Proxy & Cert

This project using websocket-client to establish connection, support proxy and custom cert file. You can pass proxy and cert to httpie.

Support http, socks4, socks4a, socks5 and socks5h proxy protocol.

http wss://echo.websocket.org --proxy=http://proxy.com
http wss://echo.websocket.org --proxy=socks4://proxy.com
http wss://echo.websocket.org --proxy=socks4a://proxy.com
http wss://echo.websocket.org --proxy=socks5://proxy.com
http wss://echo.websocket.org --proxy=socks5h://proxy.com

Custom cert file same as httpie.

http wss://yourservice.com --cert=/path/to/cert --cert-key=/path/to/cert-key --cert-key-pass=pass

Headers

Also support custom headers, you can send header through httpie.

Note wss://echo.websocket.org does not support any authentication, and will ignore any headers you send.

http wss://echo.websocket.org Custom-Header:Custom-value

Subprotocol

You can send subprotocols from the headers. Multiple subprotocols need to be separated by commas, since httpie receives only the first one with the same headers key.

http wss://echo.websocket.org Sec-WebSocket-Protocol:sub1,sub2

Auth

Support pass auth option and auth-type.

basic: httpie use basic auth as default.

http wss://echo.websocket.org --auth=user1:pass

Websocket server will receive a header like 'Authorization': 'Basic dXNlcjE6dGVzdA=='.

bearer: similar with basic.

http wss://echo.websocket.org --auth-type=bearer --auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Websocket server will receive a header like 'Authorization': 'Bearer eyxxxx'

digest: Technically, digest authentication is not supported, but you can generate an auth header manually if you want.

http wss://echo.websocket.org "Authorization: Digest username='user', realm='example', nonce='c3a7f38c-5e5a-45b2-a5b5-3b5e2c5c5c5c', uri='/path/to/protected/resource', response='generated_response', qop=auth, nc=00000001, cnonce='generated_cnonce', opaque='6d6b8f8f-6b8f-6b8f-6b8f-6b8f6b8f6b8f'"

Session

Support session option and perform as a header.

http wss://echo.websocket.org -s user1

Similar like basic auth, server will receive a header like 'Authorization': 'Basic dXNlcjE6dGVzdA=='.

Verify

You can disable SSL verification by using the --verify=no option

http wss://echo.websocket.org --verify=no

Timeout

Pass time out option to waiting for connection establish.

http wss://echo.websocket.org --timeout=3

Messages Download

Support download messages in bytes for httpie --download option. Including send and receive messages.

http wss://echo.websocket.org --download -o msgs.txt
< Request served by 1781505b56ee58
> Connected to wss://echo.websocket.org
Type a message and press enter to send it
Press Ctrl+C to close the connection
> hello              <-- sent message
< hello              <-- received message
>                    <-- waiting for input

When you press Ctrl+C, you will see:

> ^C
Oops! Disconnecting. Need to force quit? Press again!
HTTP/1.1 200
connection: Upgrade
date: Wed, 21 Aug 2024 09:19:00 GMT
fly-request-id: 01J5T3PY9EYSR9R7H6X810X4XK-nrt
sec-websocket-accept: cUDDauCuW1/u9RS5Nbcw7bYUl/8=
server: Fly/a7508dd9 (2024-08-20)
upgrade: websocket
via: 1.1 fly.io

Downloading to msgs.txt
Done. 35 bytes in 00:0.09973 (350.95880538904555 bytes/s)

And in the msgs.txt file, you can see all sent and received messages include the > and < tags.

< Request served by 1781505b56ee58    <-- echo.websocket.org connection msg
> test                                <-- sent msg
< test                                <-- received msg

If connection was closed by server, httpie will also download the messages.

Multi-line Input Support

Coming soon.

Uninstall

If you want to uninstall this plugin, use the same way when you install.

Installed by httpie command, uninstall by

httpie plugins uninstall httpie-websockets

Installed by pip command, uninstall by

pip uninstall httpie-websockets

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

httpie_websockets-0.5.4.tar.gz (12.6 kB view hashes)

Uploaded Source

Built Distribution

httpie_websockets-0.5.4-py3-none-any.whl (10.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page