I want to send a POST request (for example, with the 'request' module), but I don't find a way of sending unparsed data*.
*unparsed data => copied directly from the Chrome dev tool. Something like: tipo_accion=3&filtro=descarga&fecha=actual
It would also do the trick some way of translating that string to JSON.
I've tried so far with...
var request = require('request');
request.post({ url: 'https://target.com/form/', form: 'tipo_accion=3&filtro=descarga&fecha=actual' },
function (error, response, body) {
console.log(body)
}
);
... but it didn't work.
Firstly you should understand the difference between the request methods post and get.
The structure that you want to send:
tipo_accion=3&filtro=descarga&fecha=actual is telling me that you want to use a get request. So the proper code for that will be something like:
request(
'https://target.com/form/&tipo_accion=3&filtro=descarga&fecha=actual',
function (error, response, body) {
console.log(body)
},
);
But if it is a post request then you should use the json format
request.post({
url: 'https://target.com/form/', form: {
tipo_accion: 3,
filtro: 'descarga',
fecha: 'actual'
},
function(error, response, body) {
console.log(body)
}
);
You can convert form_data to string format and it works for me you can try it :
const request = require('request-promise');
var j = request.jar()
var data_fom = `a=value_a&b=value_b&c=value_c`
request({
url:"https://target.com/form/",
jar:j,
method:"POST",
resolveWithFullResponse: true,
form:data_form
})
Related
I have the following API response named "body" and trying to log a specific field/submap, for example the "response" map. I don't really know the correct syntax in NodeJS or JavaScript.
{"get":"fixtures","parameters":{"league":"135","season":"2022","status":"NS","timezone":"Europe\/rome"},"errors":[],"results":40,"paging":{"current":1,"total":1},"response":[{"fixture":{"id":881790,"referee":"M. Piccinini","timezone":"Europe\/rome","date":"2022-08-20T18:30:00+02:00","timestamp":1661013000,"periods":{"first":null,"second":null},"venue":{"id":943,"name":"Stadio Olimpico Grande Torino","city":"Torino"},"status":{"long":"Not Started","short":"NS","elapsed":null}},"league":{"id":135,"name":"Serie A","country":"Italy","logo":"https:\/\/media.api-sports.io\/football\/leagues\/135.png","flag":"https:\/\/media.api-sports.io\/flags\/it.svg","season":2022,"round":"Regular Season - 2"},"teams":{"home":{"id":503,"name":"Torino","logo":"https:\/\/media.api-sports.io\/football\/teams\/503.png","winner":null},"away":{"id":487,"name":"Lazio","logo":"https:\/\/media.api-sports.io\/football\/teams\/487.png","winner":null}},"goals":{"home":null,"away":null},"score":{"halftime":{"home":null,"away":null},"fulltime":{"home":null,"away":null},"extratime":{"home":null,"away":null},"penalty":{"home":null,"away":null}}},]}
The code is below:
var request = require("request");
var options = {
method: 'GET',
url: 'https://v3.football.api-sports.io/fixtures?league=135&season=2022&status=NS&timezone=Europe/rome',
qs: {},
headers: {
'x-rapidapi-host': 'v3.football.api-sports.io',
'x-rapidapi-key': 'xxxxxxxxxxxxxxxxx'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
I tried console.log(body.response); without success. I also tried body["response"] ending with "undefined" result.
It seems body was a String result so I could select the field with JSON syntax. I solved with console.log(JSON.parse(body).response[0]);
Two approaches:
Approach 1: Set the Content-Type as application/json in the server.
Approach 2: Convert the body from string to JSON object by JSON.parse
I'm sending GET requests like this in Node JS in a loop
request({
url : 'https://somelink.com',
method: 'GET'
},
function (error, response, body) {
console.log(response);
});
Since the response is async, is it possible to get the original request URL in the response?
Thanks!
You can get the original request href in the response.request object, like so:
const request = require("request");
request({
url : 'https://google.com',
method: 'GET'
},
function (error, response, body) {
if (!error) {
console.log("Original url:", response.request.uri.href);
console.log("Original uri object:", response.request.uri);
}
});
You can access more information in the request.uri object, for example:
console.log("Original uri:", response.request.uri);
This will give you some more useful information like port, path, host etc.
I am using request to get an image:
request(req.body.imageUrl, {encoding: null}, function(error, response, body) {
I then want to use the body to pass to an api that uses a multipart form and send that image (which is now in the body). I don't want to write the file to disk and then readstream from the disk again. I basically want to enter the formData of the next request by using the Buffer of the body, but it is not working.
So, for the options in the next request I have:
const options = {
method: "POST",
url: coreURL,
headers: {
"Content-Type": "multipart/form-data"
},
formData : {
file : new Buffer.from(body,'binary')
}
};
And this does not work, if I write the body to a file fs.writeFileSync(fileName, body, 'binary');
And then read in the options formData : { file : fs.createReadStream(fileName)}
it works but I cannot use the disk, so need an alternative way to pass the body into the next post as multipart form data.
Any ideas?
POC:
let request = require('request');
request.post({
url: "http://httpbin.org/anything",
formData: {
myfile: request.get("https://www.gravatar.com/avatar/f056d36f9e273fd4740ec8a5cea1348a"),
}
},
function (err, req, body) {
console.log(body);
}
);
I'm trying to get normal response using request module in node.js
and i've got problem to get response from amazon.com as normal string
i dont know why, but ive got problem only with amazon.com (eg. amazon.it, amazon.co.uk does return normal string).
const request = require('request');
request.get(
{
uri: 'https://www.amazon.com',
encoding: 'utf-8'
},
function (error, response, body) {
console.log(body)
});
The code above return something like:
b��╝��W>�S�Uk��z�=8~r����9|r|P^?}p o╗��l���ߋ�t`ޜ^]��n!��
���U�>>�#�w-z�.��O�����Oo��������y�����g�N�/��{����_>���鳟�=s���w?�z��_W)i�
��;���2��9<�0ٷ8����<=�ϱ��ղ��3�=(�"�ԯ�; �3��=�8�2;=���28����#+,3��0"�+DZ �)�2�<�
���7�(W?�8�9\?�)#'���";�ķ���ܣ�ѽ����|�8 ��╚ ��'
The response returned by Amazon is gzipped. You have to provide the gzip option to your request.
const request = require('request');
request.get(
{
uri: 'https://www.amazon.com',
gzip: true,
},
function (error, response, body) {
console.log(body)
});
I send it like so :
var url = "http://localhost:9001/v1/sanger/auth/facebook/callback",
options = {body: JSON.stringify(params), 'Content-type': 'application/json'};
request.post(url, options, function (error, response, body) {
... callbacks ...
});
I am not getting the params in the route (tried body, params and query)
When I use postman (http://cl.ly/image/473e2m173M2v) I get it in the req.body
A better way to do this (I'm assuming you've initialized your params variable somewhere else):
request = require('request');
var options = {
url: "http://localhost:9001/v1/sanger/auth/facebook/callback",
method: 'POST',
body: params,
json: true
};
request(options, function (error, response, body) {
... callbacks ...
});
You're not able to get the body because when you call JSON.stringify(params), you're converting params to a string and you don't have a json object anymore. If you send the information as plain/text but tell the request that you want json, your express app cannot verify the content-type, as could check with:
request.get('Content-Type'); // returns undefined
Since you want a json object, you shouldn't do this. Just pass the json object, like in the example above.
Then, in your route code, you can do both req.body or req.param('a_param') (for a especific key of your json) to get those values.