I mean, i'm trying to make the most simple upload ever, with the following code:
var http = require('http');
var request = http.request({
method: 'post',
host: 'https://www.ws-ti.4bank.com',
path: '/img/create_file',
headers: form.getHeaders()
});
form.pipe(request);
request.on('response', function(res) {
console.log(res.statusCode);
});
The point is, if i make a post like this, what it'll post exacly?
Does i need to put at least one header parameter or this create some kind of template?
If i need to put at least one parameter, i would put it into the form.getHeaders"()"?
Through headers you should send HTTP headers of the request as an object
Example code:
var request = http.request({
method: 'post',
host: 'https://www.ws-ti.4bank.com',
path: '/img/create_file',
headers: {
'Accept': 'text/html',
}
});
full list of the header can be found here
You can send data as JSON with the request as follows:
const data = JSON.stringify({
text: 'Hello World'
});
request.write(data);
request.end();
Related
const req = httpMocks.createRequest({
url: path,
params: {},
path: path,
headers: headers
});
This is a success request using httpsMocks. How can i create a error request for the same?
i tried but not getting output
I have an HTTP POST request that accepts the body as form-data.
grant_type: client_credentials
when I use this API in an application, I need to pass a client_id and client_secret parameter to this call.
so far I have
const postRequest = {
url: 'https://address',
method: 'POST',
headers: {
'authorization': 'Basic xyz',
'content-type': 'application/x-www-form-urlencoded'
},
formData: {
'grant_type': 'client_credentials'
}
};
How do I include the id and secret into this request?
I have also tried
formData: {
'grant_type' : 'client_credentials',
'client_id' :'id',
'client_secret' : 'secret'
}
that does not work either as stated in How to use FormData for AJAX file upload?
This is an OAuth2 flow, and it's most likely you need to pass this in your Basic authorization header.
headers: {
'authorization': 'Basic ' + btoa(`${clientId}:${clientSecret}`),
'content-type': 'application/x-www-form-urlencoded'
},
It's even better to use a good existing OAuth2 library to handle the heavy lifting. I've open sourced mine:
https://github.com/badgateway/oauth2-client
it would work like this:
const client = new OAuth2Client({
tokenEndpoint: 'https://address',
clientId: '...',
clientSecret: '...',
});
const result = await client.clientCredentials();
Hello I have tried to use the instagram api to get a connection token. I first tested it on postman and this is what I did:
I used this link to make a request post to the instagram api:
https://api.instagram.com/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=thecode
The api gives me an error: Missing required field client_id
But when I set the content type to x-www-form-urlencoded everything works fine on postman.
So I tried to do the same thing in javascript with the node module request. I tried to do the same thing as on postman with the module but it does not work... Here is my code:
request(`https://api.instagram.com/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=` + code, {
method: 'POST',
headers: {"Content-Type": "x-www-form-urlencoded"}
}, (error, response, body) => {
console.log('body:', body)
})
As per MDN, the content type should be application/x-www-form-urlencoded
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
Update:
You should read the node doc : https://nodejs.dev/learn/making-http-requests-with-nodejs
Get method:
const https = require('https');
const options = {
hostname: 'api.instagram.com',
path: '/oauth/access_token?client_id=clientid&client_secret=clientsecret&grant_type=authorization_code&redirect_uri=https://mysite/&code=thecode',
method: 'GET',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "Accept-Encoding"
}
};
const req = https.request(options, (res) => {
// ...
});
Post method:
var headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "Accept-Encoding"
};
var options = {
url: 'https://api.instagram.com/oauth/access_token',
method: 'POST',
headers: headers
};
var form = {
grant_type:'urn:ietf:params:oauth:grant-type:jwt-bearer',
client_id: 'id',
client_secret: 'secret'
redirect_uri : 'https://mysite/&code=thecode'
};
var request = https.request(options, function(response) {
// do stuff
});
request.write(querystring.stringify(form));
request.end();
I need to use http internal NodeJS library to make a POST request, how can i attach body object with this request? When I'm looking into http.RequestOptions I don't see any data or body property :/
import * as http from "http";
const options2: http.RequestOptions = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(null)
},
};
http.request(options, (res: http.IncomingMessage) => {
});
thanks for any help!
http.request returns an http.ClientRequest object.
You need to call the write method on that and pass in your body data.
I have this code in my node backend server:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
And this is the api i.e. i want to call:
router.post('/update/:_id', function(req, res, next) {
console.log("req.body:" + req.body);
}
This is my post request through angular 1:
var data = {something: "something"}
var url = 'http://localhost:8081/update/5982168b399ccf32ad75ce2e';
$http({
withCredentials: false,
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: data
})
.then(function (response) {
if (response.data) {
console.log("Post Data Submitted Successfully!");
}
}, function (response) {
console.log("Error status: " + response.status)
});
I am using Angular 1.6.1 version.
I have tried everything and looked through countless posts but none helped me. I can see the body going through. But i got undefined req.body in the node side backend.
Any help would be appreciated.
Thanks.
it could be the
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
your data is in JSON format, not x-www-form-urlencoded
{something: "something"}
you could try this
headers: {'Content-Type': 'application/json'}
by default you send JSON data in $http POST, so you may not need the header
just take it out and try
===============================================================
also, your data may not be in valid format
JS does not know what something is, it's not a variable right?
you have to make it a string, even if its the key of a { "key": "value" } pair
you could try this if above doesn't work
{
"something": "something"
}
to verify your JSON
https://jsonlint.com/
You need to stringify the data, and change the content type as was already said:
var data = {something: "something"}
var url = 'http://localhost:8081/update/5982168b399ccf32ad75ce2e';
$http({
withCredentials: false,
method: 'POST',
url: url,
headers: {'Content-Type': 'application/json'},
data: JSON.stringify(data)
})
You should in addition make sure that you can make a POST to that url using Postman and that the body is received to discart a problem in the server. On a side note, consider using Angular Resource.