Pure JavaScript Send POST Data Without a Form

You can send it and insert the data to the body: var xhr = new XMLHttpRequest(); xhr.open(“POST”, yourUrl, true); xhr.setRequestHeader(‘Content-Type’, ‘application/json’); xhr.send(JSON.stringify({ value: value })); By the way, for get request: var xhr = new XMLHttpRequest(); // we defined the xhr xhr.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status == 200) … Read more

How to post data in PHP using file_get_contents?

Sending an HTTP POST request using file_get_contents is not that hard, actually : as you guessed, you have to use the $context parameter. There’s an example given in the PHP manual, at this page : HTTP context options (quoting) : $postdata = http_build_query( array( ‘var1’ => ‘some content’, ‘var2’ => ‘doh’ ) ); $opts = … Read more

How to POST raw whole JSON in the body of a Retrofit request?

The @Body annotation defines a single request body. interface Foo { @POST(“/jayson”) FooResponse postJson(@Body FooRequest body); } Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request. public class FooRequest { final String foo; final String bar; FooRequest(String foo, String bar) { this.foo = … Read more

Send request to cURL with post data sourced from a file

You’re looking for the –data-binary argument: curl -i -X POST host:port/post-file \ -H “Content-Type: text/xml” \ –data-binary “@path/to/file” In the example above, -i prints out all the headers so that you can see what’s going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without … Read more

PHP, cURL, and HTTP POST example?

<?php // // A very simple PHP example that sends a HTTP POST to a remote site // $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,”http://www.example.com/tester.phtml”); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, “postvar1=value1&postvar2=value2&postvar3=value3”); // In real life you should use something like: // curl_setopt($ch, CURLOPT_POSTFIELDS, // http_build_query(array(‘postvar1’ => ‘value1’))); // Receive server response … curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = … Read more

How to process POST data in Node.js?

You can use the querystring module: var qs = require(‘querystring’); function (request, response) { if (request.method == ‘POST’) { var body = ”; request.on(‘data’, function (data) { body += data; // Too much POST data, kill the connection! // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) … Read more

How do you handle multiple submit buttons in ASP.NET MVC Framework?

Here is a mostly clean attribute-based solution to the multiple submit button issue based heavily on the post and comments from Maarten Balliauw. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class MultipleButtonAttribute : ActionNameSelectorAttribute { public string Name { get; set; } public string Argument { get; set; } public override bool IsValidName(ControllerContext controllerContext, … Read more