httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
33.22k stars 3.67k forks source link

Sometimes, I really don't want JSON or key values pairs..... #247

Closed shaleh closed 10 years ago

shaleh commented 10 years ago

Now and then I need to talk SOAP. I know, yuck :-) I can do this with curl by passing my SOAP body via -d/--data. I could not find a way to accomplish the same thing with httpie so I hacked this in real quick. Sorry, I do not have the ability to fork and offer a pull request at the moment. Besides, maybe I missed something and this patch is not needed.

If this were to be properly included I suspect a warning/error when both foo=bar and --data is used.

Making the function used to populate args.data from items smarter would require adding some magic keyword like 'rawdata:=blah' and ensuring the parser does not attempt to inspect blah at all. This is why I added a --data option instead.

If this functionality really is missing I can fork and make a proper pull request when time allows.

Note my example script at the end makes it look like potentially the data could be read from a file. But I would really like to keep it all contained in the script. However, I could see extending --data to allow the input to be read from a file like the positional arguments do.

diff --git a/httpie/cli.py b/httpie/cli.py
index 9b7a454..7d3a025 100644
--- a/httpie/cli.py
+++ b/httpie/cli.py
@@ -152,6 +152,16 @@ content_type = parser.add_argument_group(
 )

 content_type.add_argument(
+    '--data',
+    dest='data_string',
+    default='',
+    help="""
+    Send this as the data parameter to Requests. Ignores all other data
+    items on command line.
+
+    """
+)
+content_type.add_argument(
     '--json', '-j',
     action='store_true',
     help="""
diff --git a/httpie/client.py b/httpie/client.py
index 6444c17..03d1bcf 100644
--- a/httpie/client.py
+++ b/httpie/client.py
@@ -72,16 +72,19 @@ def get_requests_kwargs(args, base_headers=None):
     Translate our `args` into `requests.request` keyword arguments.

     """
-    # Serialize JSON data, if needed.
-    data = args.data
-    auto_json = data and not args.form
-    if args.json or auto_json and isinstance(data, dict):
-        if data:
-            data = json.dumps(data)
-        else:
-            # We need to set data to an empty string to prevent requests
-            # from assigning an empty list to `response.request.data`.
-            data = ''
+    if args.data_string:
+        data = args.data_string  # raw, no processing applied
+    else:
+        # Serialize JSON data, if needed.
+        data = args.data
+        auto_json = data and not args.form
+        if args.json or auto_json and isinstance(data, dict):
+            if data:
+                data = json.dumps(data)
+            else:
+                # We need to set data to an empty string to prevent requests
+                # from assigning an empty list to `response.request.data`.
+                data = ''

     # Finalize headers.
     headers = get_default_headers(args)

It looks like this in use:

#!/bin/bash

URL=https://example.com/SOAP

read -r -d '' SOAP <<'EOF'
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="http://example.com/">
      <UserName>me@example.com</UserName>
      <Password>p@ssw0rd</Password>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <SomeAPIMethod xmlns="http://example.com/" />
  </soap:Body>
</soap:Envelope>
EOF

http --data "$SOAP" POST "$URL" 'SoapAction:http://example.com/SomeAPIMethod' 'Host:soap.example.com' 'Content-Type:text/xml; charset=utf-8'
jkbrzt commented 10 years ago

Thanks! But have you tried passing the data via stdin?

$ echo $SOAP | http POST "$URL" 'SoapAction:http://example.com/SomeAPIMethod' 'Host:soap.example.com' 'Content-Type:text/xml; charset=utf-8'
shaleh commented 10 years ago

Thanks. I figured I was missing something. I still see value in the command line option though. If you do too I'd be happy to submit a pull request.

On August 8, 2014 4:34:03 PM PDT, "Jakub Roztočil" notifications@github.com wrote:

Thanks! But have you tried something passing the data via stdin?

$ echo $SOAP | http POST "$URL"
'SoapAction:http://example.com/SomeAPIMethod' 'Host:soap.example.com'
'Content-Type:text/xml; charset=utf-8'

Reply to this email directly or view it on GitHub: https://github.com/jakubroztocil/httpie/issues/247#issuecomment-51668933

Sent from my Android device with K-9 Mail. Please excuse my brevity.