If you are a developer, you might already know what an HTTP POST request is. You are pretty comfortable in making one. The probability that your present project is somehow related to one or more HTTP post requests, is pretty damn high. Most of the modern applications interact with a server. Here I will show different ways of making a post request in different languages.
How to make HTTP POST Request using cURL
We programmers love to read codes. So I will directly jump into code. Here is the code snippet you can run in your terminal to make one POST request using curl.
curl -X POST -H "Content-Type: application/json" -d '{
"email": "[email protected]",
"password": "12345678"
}' "http://example.com/api/auth"
How to make HTTP POST Request using jQuery AJAX
var settings = {
"async": true,
"crossDomain": true,
"url": "http://example.com/api/auth",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"processData": false,
"data": '{"email": "[email protected]","password": "12345678"}'
}
$.ajax(settings).done(function (response) {
console.log(response);
});
How to make HTTP POST Request using HTML5 Fetch API
var data = {
email: "[email protected]",
password: "12345678"
};
data = JSON.stringify(data);
fetch("http://example.com/api/auth", {
method: "POST",
body: data,
headers: {
"Content-Type": "application/json"
}
})
.then(function (res) {
return res.json();
})
.then(function (res) {
console.log(res);
});
How to make HTTP POST Request using XMLHttpRequest
var data = JSON.stringify({
"email": "[email protected]",
"password": "12345678"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://example.com/api/auth");
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);
How to make HTTP POST Request in NodeJS using Native Http Module
var http = require("http");
var options = {
"method": "POST",
"hostname": "example.com",
"port": null,
"path": "/api/auth",
"headers": {
"content-type": "application/json"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(JSON.stringify({ email: '[email protected]', password: '12345678' }));
req.end();
How to make HTTP POST Request in NodeJs using Request NPM Package
var request = require("request");
var options = { method: 'POST',
url: 'http://example.com/api/auth',
headers: {
'content-type': 'application/json'
},
body: {
email: '[email protected]',
password: '12345678'
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
How to make HTTP POST Request in PHP (HttpRequest)
<?php
$request = new HttpRequest();
$request->setUrl('http://example.com/api/auth');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'content-type' => 'application/json'
));
$request->setBody('{
"email": "[email protected]",
"password": "12345678"
}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
?>
How to make HTTP POST Request in PHP (cURL)
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://example.com/api/auth",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 20,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"email": "[email protected]","password": "12345678"}',
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
How to make HTTP POST Request in python3
import http.client
conn = http.client.HTTPConnection("example.com")
payload = '{"email": "[email protected]","password": "12345678"}'
headers = {
'content-type': "application/json",
}
conn.request("POST", "/api/auth", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
How to make HTTP POST Request in python (requests module)
import requests
url = "http://example.com/api/auth"
payload = '{"email":"[email protected]","password": "12345678"}'
headers = {
'content-type': "application/json"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
How to make HTTP POST Request in JAVA using OkHttp Library
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{
\"email\": \"[email protected]\",
\"password\": \"12345678\"}");
Request request = new Request.Builder()
.url("http://example.com/api/auth")
.post(body)
.addHeader("content-type", "application/json")
.build();
Response response = client.newCall(request).execute();
I hope that you enjoyed reading the code as same as me I did while writing them. I will try to update this article as I get to learn more about other languages. I have an advice for you. Please stop using XMLHttpRequest, rather use Fetch API.
If you like to contribute here, kindly leave a comment. Thank you.
The post Eleven Different Ways – How to make one HTTP POST Request appeared first on Apply Head.