Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
    
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 at 12:42
    
Dont you understand the question? this is the best i can explain. the title and body are clear for me –  Viswanath L Feb 22 at 12:45
1  
@Peter Taylor I have edited the question to try to make it more clear. –  ace Feb 22 at 12:52
    
Thanks for that :) –  Viswanath L Feb 22 at 12:55
    
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 at 14:08
show 14 more comments

16 Answers

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
share|improve this answer
add comment

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"]
share|improve this answer
add comment

Javascript (84 characters):

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

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

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

newLISP - 58 characters

(post-url "http://www.example.com/target""String to post")
share|improve this answer
add comment

Smalltalk (Smalltalk/X), 64 71

HTTPInterface post:'http://www.example.com/target'with:'String to post'
share|improve this answer
    
this contains 71 characters –  Viswanath L Feb 23 at 5:05
    
sorry - you're right - I tested (and counted) another string to post. Embarrased! –  blabla999 Feb 24 at 21:11
add comment

Powershell v3 - 58

iwr http://www.example.com/target -b "String to post"-me 3
share|improve this answer
add comment

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.

share|improve this answer
    
There is a solution I wasn't expecting. Well done! –  Ismael Miguel Feb 24 at 21:03
    
You need to use www.example.com, not example.com. –  toothbrush Feb 27 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 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 at 13:32
add comment

Lua (texlua), 73 bytes

require("socket.http").request("www.example.com/target","String to post")
share|improve this answer
    
The question said to POST to http://www.example.com/target. –  toothbrush Feb 27 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 at 13:37
    
Great! I know that you can omit the http:// part - I wasn't saying to include it. –  toothbrush Feb 27 at 13:48
add comment

Game Maker Language (GMStudio), 66

http_post_string("http://www.example.com/target","String to post")
share|improve this answer
add comment

VBScript: 114 characters

dim x:set x=createobject("MSXML2.ServerXMLHTTP")
x.open"post","http://example.com/target"
x.send"String to post"
share|improve this answer
1  
Slightly smaller: With CreateObject("MSXML2.ServerXMLHTTP"):.Open"POST","http://example.com/target":.Se‌​nd"String to post":End With –  toothbrush Feb 22 at 20:35
    
Yes, you're right. Thank you for your comment! –  Alexey Feb 22 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 at 13:34
add comment

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")
share|improve this answer
    
If you want to gain another char: __import__('urllib2').urlopen("example.com/target","String to post") –  nutria Feb 23 at 18:19
    
Sneaky. I like it –  James_pic Feb 24 at 22:28
add comment

Bash - 63 chars

wget --post-data="String to post" http://www.example.com/target
share|improve this answer
add comment

PHP (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!

share|improve this answer
    
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 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 at 13:39
    
The answer was removed, but still can be seen on the edit option. –  Ismael Miguel Feb 24 at 13:40
    
But surely the Content-Type is text/plain, not application/x-www-form-urlencoded? –  TRiG Feb 25 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 at 12:23
add comment

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.

share|improve this answer
add comment

jQuery

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

share|improve this answer
add comment

//Actionscript 3 //length: 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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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