# Shortest code to post a string to WebServer

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.

• 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. – Peter Taylor Feb 22 '14 at 12:42
• Dont you understand the question? this is the best i can explain. the title and body are clear for me – Viswanath Lekshmanan Feb 22 '14 at 12:45
• @Peter Taylor I have edited the question to try to make it more clear. – user12205 Feb 22 '14 at 12:52
• 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.) – manatwork Feb 22 '14 at 14:08
• @manatwork why don't you post it as an answer? – user12205 Feb 22 '14 at 14:17

# 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

• That's Bash running on Linux. I have a Bash commandline for my windows box but do not have /dev – Jerry Jeremiah Jan 27 '15 at 4:07
• 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.) – manatwork Jan 27 '15 at 9:30
• 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. – Jerry Jeremiah Jan 28 '15 at 3:01
• For enybody else that never knew you could do that, look here: linuxjournal.com/content/… – Jerry Jeremiah Jan 28 '15 at 3:05

# 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"]


# Powershell v3 - 58

iwr http://www.example.com/target -b "String to post"-me 3


# 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.

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

### newLISP - 58 characters

(post-url "http://www.example.com/target""String to post")


# Smalltalk (Smalltalk/X), 64 71

HTTPInterface post:'http://www.example.com/target'with:'String to post'

• this contains 71 characters – Viswanath Lekshmanan Feb 23 '14 at 5:05
• sorry - you're right - I tested (and counted) another string to post. Embarrased! – blabla999 Feb 24 '14 at 21:11

# Bash - 63 chars

wget --post-data="String to post" http://www.example.com/target


## 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.

• There is a solution I wasn't expecting. Well done! – Ismael Miguel Feb 24 '14 at 21:03
• You need to use www.example.com, not example.com. – Toothbrush Feb 27 '14 at 12:36
• @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). – primo Feb 27 '14 at 13:18
• @primo Yes, that's true. However, the question did originally say that the code should POST to http://www.example.com/target. – Toothbrush Feb 27 '14 at 13:32

## Lua (texlua), 73 bytes

require("socket.http").request("www.example.com/target","String to post")

• The question said to POST to http://www.example.com/target. – Toothbrush Feb 27 '14 at 13:34
• @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. – Heiko Oberdiek Feb 27 '14 at 13:37
• Great! I know that you can omit the http:// part - I wasn't saying to include it. – Toothbrush Feb 27 '14 at 13:48

# Tcl - 85 chars

package require http
http::geturl "//www.example.com/target" -query "String to post"


## Game Maker Language (GMStudio), 66

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


# VBScript: 114 characters

dim x:set x=createobject("MSXML2.ServerXMLHTTP")
x.open"post","http://example.com/target"
x.send"String to post"

• Slightly smaller: With CreateObject("MSXML2.ServerXMLHTTP"):.Open"POST","http://example.com/target":.Send"String to post":End With – Toothbrush Feb 22 '14 at 20:35
• Yes, you're right. Thank you for your comment! – user15287 Feb 22 '14 at 20:55
• The question now states that you need to POST to http://www.example.com/target. Note the www. part. – Toothbrush Feb 27 '14 at 13:34

# 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")

• If you want to gain another char: __import__('urllib2').urlopen("example.com/target","String to post") – Andrea Ciceri Feb 23 '14 at 18:19
• Sneaky. I like it – James_pic Feb 24 '14 at 22:28

# jQuery, 58

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

• jQuery isn't a programming language... – Downgoat Jan 30 '16 at 2:50

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

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


Try it here (Firefox only).

Uses jQuery's post function.

## 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;


no error handling

# 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')"


# 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'))));


• 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. – primo Feb 24 '14 at 5:04
• That's why I said that I'm not sure if it works. But I will remove the second answer. – Ismael Miguel Feb 24 '14 at 13:39
• The answer was removed, but still can be seen on the edit option. – Ismael Miguel Feb 24 '14 at 13:40
• But surely the Content-Type is text/plain, not application/x-www-form-urlencoded? – TRiG Feb 25 '14 at 3:30
• 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. – Ismael Miguel Feb 25 '14 at 12:23

# 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.