Shortest code to post a string to WebServer

5

1

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.

Viswanath Lekshmanan

Posted 2014-02-22T12:35:45.440

Reputation: 157

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 – 2014-02-22T12:42:03.787

Dont you understand the question? this is the best i can explain. the title and body are clear for me – Viswanath Lekshmanan – 2014-02-22T12:45:40.700

1@Peter Taylor I have edited the question to try to make it more clear. – user12205 – 2014-02-22T12:52:29.913

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 – 2014-02-22T14:08:17.683

@manatwork why don't you post it as an answer? – user12205 – 2014-02-22T14:17:05.373

@ace, I felt it's not valuable enough for an answer. – manatwork – 2014-02-22T14:47:54.087

Is it sufficient that the request be permitted by the HTTP spec (and if so, which?), or is there a set of additional minimal headers to make it useful to the server? – Peter Taylor – 2014-02-22T15:04:15.460

I've reduced my answer by 3 bytes. Can you check it? – Ismael Miguel – 2014-02-23T05:17:03.703

Yeah, updated... :) – Viswanath Lekshmanan – 2014-02-23T05:26:21.527

I've reduced my answer by 9 characters! – Toothbrush – 2014-02-24T10:36:10.387

@toothbrush Yeah updaetd that :) – Viswanath Lekshmanan – 2014-02-24T13:39:57.217

Some of the answers are posting to example.com, and some to www.example.com. You may wish to clarify that requirement, one way or the other. – TRiG – 2014-02-25T03:31:10.833

@ViswanathL Yes, I suggest you clarify. – Toothbrush – 2014-02-25T11:04:53.963

Yeah i updated the url, Please change and update th answers accordingly – Viswanath Lekshmanan – 2014-02-25T11:30:25.260

@ViswanathL OK. I've updated my solution, too. Do we really need the http: part, too? – Toothbrush – 2014-02-25T18:36:45.427

@toothbrush. The http: part is the protocol. I'd presume that if your language infers the protocol, that's fine. But there is a real difference between example.com and www.example.com: they're two different names. – TRiG – 2014-02-26T14:18:14.950

Answers

16

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

manatwork

Posted 2014-02-22T12:35:45.440

Reputation: 17 865

That's Bash running on Linux. I have a Bash commandline for my windows box but do not have /dev – Jerry Jeremiah – 2015-01-27T04:07:58.573

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 – 2015-01-27T09:30:10.463

1I'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 – 2015-01-28T03:01:08.383

For enybody else that never knew you could do that, look here: http://www.linuxjournal.com/content/more-using-bashs-built-devtcp-file-tcpip

– Jerry Jeremiah – 2015-01-28T03:05:20.607

9

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

draegtun

Posted 2014-02-22T12:35:45.440

Reputation: 1 592

5

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.

Toothbrush

Posted 2014-02-22T12:35:45.440

Reputation: 3 197

1I think you can replace http://www.example.com/target with ://.example.com/target – Clyde Lobo – 2014-02-24T09:26:01.433

3@ClydeLobo. Can you get rid of the www? I don't see how that's legitimate. – TRiG – 2014-02-25T03:27:37.260

@TRiG OK. It's now fully compliant with the question. – Toothbrush – 2014-02-26T13:51:23.010

5

Powershell v3 - 58

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

Rynant

Posted 2014-02-22T12:35:45.440

Reputation: 2 353

3

Lua (texlua), 73 bytes

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

Heiko Oberdiek

Posted 2014-02-22T12:35:45.440

Reputation: 3 841

The question said to POST to http://www.example.com/target. – Toothbrush – 2014-02-27T13:34:00.330

@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 – 2014-02-27T13:37:33.607

Great! I know that you can omit the http:// part - I wasn't saying to include it. – Toothbrush – 2014-02-27T13:48:11.970

3

Smalltalk (Smalltalk/X), 64 71

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

blabla999

Posted 2014-02-22T12:35:45.440

Reputation: 1 869

this contains 71 characters – Viswanath Lekshmanan – 2014-02-23T05:05:32.750

sorry - you're right - I tested (and counted) another string to post. Embarrased! – blabla999 – 2014-02-24T21:11:19.537

3

newLISP - 58 characters

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

cormullion

Posted 2014-02-22T12:35:45.440

Reputation: 569

3

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.

primo

Posted 2014-02-22T12:35:45.440

Reputation: 30 891

There is a solution I wasn't expecting. Well done! – Ismael Miguel – 2014-02-24T21:03:32.230

You need to use www.example.com, not example.com. – Toothbrush – 2014-02-27T12:36:05.283

@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 – 2014-02-27T13:18:02.690

@primo Yes, that's true. However, the question did originally say that the code should POST to http://www.example.com/target. – Toothbrush – 2014-02-27T13:32:40.120

3

Bash - 63 chars

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

David Wilkins

Posted 2014-02-22T12:35:45.440

Reputation: 593

3

Tcl - 85 chars

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

P̲̳x͓L̳

Posted 2014-02-22T12:35:45.440

Reputation: 213

2

VBScript: 114 characters

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

user15287

Posted 2014-02-22T12:35:45.440

Reputation:

1Slightly smaller: With CreateObject("MSXML2.ServerXMLHTTP"):.Open"POST","http://example.com/target":.Send"String to post":End With – Toothbrush – 2014-02-22T20:35:42.997

Yes, you're right. Thank you for your comment! – None – 2014-02-22T20:55:07.190

The question now states that you need to POST to http://www.example.com/target. Note the www. part. – Toothbrush – 2014-02-27T13:34:40.927

2

Game Maker Language (GMStudio), 66

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

Timtech

Posted 2014-02-22T12:35:45.440

Reputation: 12 038

2

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

James_pic

Posted 2014-02-22T12:35:45.440

Reputation: 3 988

If you want to gain another char: import('urllib2').urlopen("http://www.example.com/target%22,%22String to post")

– Andrea Ciceri – 2014-02-23T18:19:20.853

Sneaky. I like it – James_pic – 2014-02-24T22:28:53.670

2

jQuery, 58

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

Stephen Melvin

Posted 2014-02-22T12:35:45.440

Reputation: 183

jQuery isn't a programming language... – Downgoat – 2016-01-30T02:50:16.223

2

, 47 chars / 51 bytes (noncompetitive)

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

Try it here (Firefox only).

Uses jQuery's post function.

Mama Fun Roll

Posted 2014-02-22T12:35:45.440

Reputation: 7 234

1

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!

Ismael Miguel

Posted 2014-02-22T12:35:45.440

Reputation: 6 797

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 – 2014-02-24T05:04:58.297

That's why I said that I'm not sure if it works. But I will remove the second answer. – Ismael Miguel – 2014-02-24T13:39:35.097

The answer was removed, but still can be seen on the edit option. – Ismael Miguel – 2014-02-24T13:40:46.287

But surely the Content-Type is text/plain, not application/x-www-form-urlencoded? – TRiG – 2014-02-25T03:30:07.237

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 – 2014-02-25T12:23:24.333

1

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.

Ismael Miguel

Posted 2014-02-22T12:35:45.440

Reputation: 6 797

1

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

Mauro

Posted 2014-02-22T12:35:45.440

Reputation: 35

1

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

Robert Talada

Posted 2014-02-22T12:35:45.440

Reputation: 1