6
\$\begingroup\$

In , this code posts String to post to http://www.example.com/target:

curl -d "String to post" "http://www.example.com/target"

NOTE: You should use http://www.example.com/target as the URL to POST to, without any change.

I think all other languages that I have knowledge in need a lot more code than this. I'm not blaming other languages. Let's see the interesting answers that reduce the effort.

Your task is to write the shortest code possible to send a POST request to a sample server, http://www.example.com/target, and the request data should be the string String to post.

\$\endgroup\$
16
  • \$\begingroup\$ It's not clear that the title and the body are asking for the same thing, let alone what you consider to be sufficient and necessary to qualify as a solution. \$\endgroup\$ Feb 22, 2014 at 12:42
  • \$\begingroup\$ Dont you understand the question? this is the best i can explain. the title and body are clear for me \$\endgroup\$ Feb 22, 2014 at 12:45
  • 1
    \$\begingroup\$ @Peter Taylor I have edited the question to try to make it more clear. \$\endgroup\$
    – user12205
    Feb 22, 2014 at 12:52
  • \$\begingroup\$ Another one of the same size: POST http://www.example.com/target<<<'String to send'. (POST is either a stand-alone script or symlink to lwp-request, a command line tool delivered with the LWP perl module.) \$\endgroup\$
    – manatwork
    Feb 22, 2014 at 14:08
  • \$\begingroup\$ @manatwork why don't you post it as an answer? \$\endgroup\$
    – user12205
    Feb 22, 2014 at 14:17

19 Answers 19

16
\$\begingroup\$

Bash: 90 characters

No external tools, just pure bash.

echo 'POST /target HTTP/1.0
Content-length:14

String to post'>/dev/tcp/www.example.com/80
\$\endgroup\$
4
  • \$\begingroup\$ That's Bash running on Linux. I have a Bash commandline for my windows box but do not have /dev \$\endgroup\$ Jan 27, 2015 at 4:07
  • \$\begingroup\$ Not sure whether it depends on /dev/, but /dev/tcp/ doesn't exist anywhere – it is just Bash's own syntax for opening a TCP connection. Personally I would expect it to work regardless the presence of /dev/. It certainly works in Cygwin, but I have a feeling that you use MinGW. Could you please @JerryJeremiah post us some details about this? Like Bash's version, the package from which it comes, what actually happens when you run my posted code. (By the way, should not happen anything visible – Bash just connects, transfers, disconnects, exits with 0.) \$\endgroup\$
    – manatwork
    Jan 27, 2015 at 9:30
  • 1
    \$\begingroup\$ I'm astonished to announce this works - even without a /dev/ directory! I saw the /dev/ and jumped to a stupid conclusion. I am running the bash version 3.1.17 from the bmatzelle/gow github repo. \$\endgroup\$ Jan 28, 2015 at 3:01
  • \$\begingroup\$ For enybody else that never knew you could do that, look here: linuxjournal.com/content/… \$\endgroup\$ Jan 28, 2015 at 3:05
9
\$\begingroup\$

Rebol (51 chars)

write http://www.example.com/target"String to post"


Above works in Rebol 3. Below is a Rebol 2 version (63 chars):

read/custom http://www.example.com/target[post"String to post"]
\$\endgroup\$
5
\$\begingroup\$

Powershell v3 - 58

iwr http://www.example.com/target -b "String to post"-me 3
\$\endgroup\$
5
\$\begingroup\$

Javascript, 84

with(XMLHttpRequest())open('POST','//www.example.com/target'),send('String to post')

Try in the console of any page served over plain HTTP.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I think you can replace http://www.example.com/target with ://.example.com/target \$\endgroup\$
    – Clyde Lobo
    Feb 24, 2014 at 9:26
  • 3
    \$\begingroup\$ @ClydeLobo. Can you get rid of the www? I don't see how that's legitimate. \$\endgroup\$
    – TRiG
    Feb 25, 2014 at 3:27
  • \$\begingroup\$ @TRiG OK. It's now fully compliant with the question. \$\endgroup\$
    – Toothbrush
    Feb 26, 2014 at 13:51
3
\$\begingroup\$

newLISP - 58 characters

(post-url "http://www.example.com/target""String to post")
\$\endgroup\$
3
\$\begingroup\$

Smalltalk (Smalltalk/X), 64 71

HTTPInterface post:'http://www.example.com/target'with:'String to post'
\$\endgroup\$
2
  • \$\begingroup\$ this contains 71 characters \$\endgroup\$ Feb 23, 2014 at 5:05
  • \$\begingroup\$ sorry - you're right - I tested (and counted) another string to post. Embarrased! \$\endgroup\$
    – blabla999
    Feb 24, 2014 at 21:11
3
\$\begingroup\$

Bash - 63 chars

wget --post-data="String to post" http://www.example.com/target
\$\endgroup\$
3
\$\begingroup\$

PHP - 100 bytes

<?fwrite(fsockopen('www.example.com',80),'POST /target HTTP/1.0
Content-length:14

String to post');

I apologize for any similarities to manatwork's bash solution, but I believe that this is the shortest way to do it.

\$\endgroup\$
4
  • \$\begingroup\$ There is a solution I wasn't expecting. Well done! \$\endgroup\$ Feb 24, 2014 at 21:03
  • \$\begingroup\$ You need to use www.example.com, not example.com. \$\endgroup\$
    – Toothbrush
    Feb 27, 2014 at 12:36
  • \$\begingroup\$ @toothbrush they do resolve to the same IP. But I agree that for fairness, everyone should be using exactly the same URL (and, that the question was modified after this was posted to make that explicit). \$\endgroup\$
    – primo
    Feb 27, 2014 at 13:18
  • \$\begingroup\$ @primo Yes, that's true. However, the question did originally say that the code should POST to http://www.example.com/target. \$\endgroup\$
    – Toothbrush
    Feb 27, 2014 at 13:32
3
\$\begingroup\$

Lua (texlua), 73 bytes

require("socket.http").request("www.example.com/target","String to post")
\$\endgroup\$
3
  • \$\begingroup\$ The question said to POST to http://www.example.com/target. \$\endgroup\$
    – Toothbrush
    Feb 27, 2014 at 13:34
  • \$\begingroup\$ @toothbrush: Thanks, I missed www.. I have updated the answer and also changed .org to .com. http:// can be omitted, because it is the default. \$\endgroup\$ Feb 27, 2014 at 13:37
  • \$\begingroup\$ Great! I know that you can omit the http:// part - I wasn't saying to include it. \$\endgroup\$
    – Toothbrush
    Feb 27, 2014 at 13:48
3
\$\begingroup\$

Tcl - 85 chars

package require http
http::geturl "//www.example.com/target" -query "String to post"
\$\endgroup\$
2
\$\begingroup\$

Game Maker Language (GMStudio), 66

http_post_string("http://www.example.com/target","String to post")
\$\endgroup\$
2
\$\begingroup\$

VBScript: 114 characters

dim x:set x=createobject("MSXML2.ServerXMLHTTP")
x.open"post","http://example.com/target"
x.send"String to post"
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Slightly smaller: With CreateObject("MSXML2.ServerXMLHTTP"):.Open"POST","http://example.com/target":.Send"String to post":End With \$\endgroup\$
    – Toothbrush
    Feb 22, 2014 at 20:35
  • \$\begingroup\$ Yes, you're right. Thank you for your comment! \$\endgroup\$
    – user15287
    Feb 22, 2014 at 20:55
  • \$\begingroup\$ The question now states that you need to POST to http://www.example.com/target. Note the www. part. \$\endgroup\$
    – Toothbrush
    Feb 27, 2014 at 13:34
2
\$\begingroup\$

Python 2: 80 chars

Just using the standard library:

import urllib2 as u
u.urlopen("http://www.example.com/target","String to post")

For an extra char, you can make the import less unusual.

import urllib2
urllib2.urlopen("http://www.example.com/target","String to post")
\$\endgroup\$
2
  • \$\begingroup\$ If you want to gain another char: __import__('urllib2').urlopen("example.com/target","String to post") \$\endgroup\$ Feb 23, 2014 at 18:19
  • \$\begingroup\$ Sneaky. I like it \$\endgroup\$
    – James_pic
    Feb 24, 2014 at 22:28
2
\$\begingroup\$

jQuery, 58

$.post("http://www.example.com/target", "String to post");

\$\endgroup\$
1
  • \$\begingroup\$ jQuery isn't a programming language... \$\endgroup\$
    – Downgoat
    Jan 30, 2016 at 2:50
2
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 47 chars / 51 bytes (noncompetitive)

ɟŏ`//www.example.com/target⍪String to post

Try it here (Firefox only).

Uses jQuery's post function.

\$\endgroup\$
1
\$\begingroup\$

Actionscript 3, 201

var aRV:URLVariables = new URLVariables();
aRV.STRING = "my string";
var aR:URLRequest = new URLRequest("http://www.example.com/target");
aR.method = URLRequestMethod.POST;
aR.data = aRV;
var aL = new URLLoader();
aL.load(aR);

no error handling

\$\endgroup\$
1
\$\begingroup\$

Batch/Powershell - 118 Chars

If you need to post from a Windows batch file or avoid changing ExecutionPolicy:

powershell -Command "(New-Object Net.WebClient).UploadString('http://www.example.com/target', 'p1=String%20to%20Post')"
\$\endgroup\$
1
\$\begingroup\$

PHP, 198 bytes

This is shamefully large...:

<?fopen('http://www.example.com/target','rb',0,stream_context_create(array('http'=>array('method'=>'POST','header'=>'Content-Type: application/x-www-form-urlencoded','content'=>'String to post'))));

At least I answered!

\$\endgroup\$
5
  • \$\begingroup\$ The second one complains that the HTTP wrapper does not support writable connections. If it were to do anything, it would attempt to overwrite the contents of the http request with the given string. \$\endgroup\$
    – primo
    Feb 24, 2014 at 5:04
  • \$\begingroup\$ That's why I said that I'm not sure if it works. But I will remove the second answer. \$\endgroup\$ Feb 24, 2014 at 13:39
  • \$\begingroup\$ The answer was removed, but still can be seen on the edit option. \$\endgroup\$ Feb 24, 2014 at 13:40
  • \$\begingroup\$ But surely the Content-Type is text/plain, not application/x-www-form-urlencoded? \$\endgroup\$
    – TRiG
    Feb 25, 2014 at 3:30
  • \$\begingroup\$ application/x-www-form-urlencoded is the default mime-type to send data to a server, text/plain is the default mime-type to receive any non-binary text without a declared mime-type. \$\endgroup\$ Feb 25, 2014 at 12:23
1
\$\begingroup\$

Bash, 69 bytes

lwp-request -m POST -b http://www.example.com/target "String to post"

You can read about the lwp-request command here: http://linux.about.com/library/cmd/blcmdl1_POST.htm

I think that the answer given by @manatwork is a much more neat and compatible version.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.