I am trying to create script that could post comment in my thread on steam with node and request lib. Trying to achieve by doing something like this:
const body = {
comment: 'Sometext',
count: '15',
sessionid: session_id,
}
bumpingDiscussionsPostsModule.bumpInDiscussion=async() => {
const postHeader = {
method: 'POST',
uri: urlPost,
headers: {
Cookie: cookie
},
form: JSON.stringify(body)
}
const response = await request(postHeader);
console.log(response);
}
Tho steam keeps returning me returning {"success":false}, any clues what I am doing wrong?
I was just formatting it wrong. If someone might be looking for it, this gets the job done:
const doBump = await fetch(bumpingUri, {
method: 'post',
body: `comment=${process.env.BUMP_COMMENT}&count=15&sessionid=${process.env.CONN_SESID}&extended_data=${process.env.EXTENDED_DATA}&feature2=${featureID}oldestfirst=true&include_raw=true`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept': 'text/javascript, text/html, application/xml, text/xml, */*',
Cookie: process.env.CONN_STRING
}
});
Related
var request = require('request');
var options = {
'method': 'POST',
'url': 'http://',
'headers': {
},
form: {
'username': '',
'password': ''
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
transforming this to axios request:
let data = qs.stringify({ 'username': '',
'password': '' })
const options = {
method: 'POST',data,
headers: { 'Content-Type': 'application/x-www-form-urlencoded'},
url: '',
};
response[0] = await Axios(options).then((res) => {
console.log("res",res)
return res.data
}
).catch((err) => {
console.log("err status", err.response.status)
return err.response.data
});
gives an error and the headers shown doesnt have the url encoded:
url: '',
method: 'post',
data: 'username=&password=',
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.19.2'
},
Why the content-type : url-encoded is not in the headers and only the Accept: 'application/json, text/plain, /' is showing.
You are not showing which error you are receiving but the way you are setting the Axios call seems right following the Axios documentation. So I suspect it is a problem on how you have set up your Node.js server.
If you are using Express.js v.4+, probably you forgot to set up in your server the directive to parse application/x-www-form-urlencoded content type as explained here in the official documentation.
app.use(express.urlencoded({ extended: true }))
If you are using the body-parser package you need to set up your server with:
app.use(bodyParser.urlencoded({extended: true}));
Don't understand why the response body of the following request is empty:
request:
req.headers['User-Agent'] = 'request';
req.headers['Accept'] = 'text/html';
const options = {
url: config.prodURL,
headers: req.headers,
qs: req.query,
method: req.method
};
request(options, (err, resp, body) => {
console.log(resp);
});
I'm expecting HTML in this response, but for some reason, when I look at resp it displays body: ''
Part of the resp contains the href url and if I use this in any client, it displays the HTML code.
What is wrong with this config?
update
Here's the data with the exception of hrefas I cannot share this URL. err is null, here are the rest of the values:
{ url: 'http://my.url/',
headers:
{ 'user-agent': 'PostmanRuntime/7.15.2',
accept: '*/*',
'cache-control': 'no-cache',
'postman-token': '8c208831-580b-4460-b3e0-046d85789658',
host: 'localhost:8001',
'accept-encoding': 'gzip, deflate',
connection: 'keep-alive',
'User-Agent': 'request',
Accept: 'text/html' },
qs: { query-params... },
method: 'GET'
}
I believe it might be related to the response headers, the URL requested doesn't return any, so does requestJs require a content-type to be able to parse the body?
I'm having issues with authentication when I change from httpreq to axios' post. I need to use multipart/form-data. For some reason it isn't working in axios, but it is in httpreq. What am I missing?
httpreq:
postType1ToServer: function(request, callback) {
var options = {
headers: {
'Content-Type': 'multipart/form-data',
Connection: 'keep-alive',
Authorization: request.type1msg
},
agent: keepaliveAgent
};
return httpreq[request.method](request.options.url, options);
versus axios
postType1ToServer: function(request, callback) {
var options = {
headers: {
'Content-Type': 'multipart/form-data',
Connection: 'keep-alive',
Authorization: request.type1msg
},
httpAgent: new http.Agent({ keepAlive: true })
};
return axios
.post(request.options.url, options)
.then(function(response) {
console.log(response);
})
.catch(function(response) {
console.log(response);
});
See the documentation:
axios.post(url[, data[, config]])
You've put the config where the data is supposed to be, and missed the data out entirely.
You can try to remove backslash form URL at end of it
How can we use bearer token with POST method using npm sync-request? The sync-request resource page has the way to use authorization in GET request but not in POST request.
*******GET Request*******
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
'headers': {
'user-agent': 'example-user-agent'
}
});
****POST Request*****
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
json: { username: 'Name' }
});
Not sure why you would want to use sync-request which can cause timing issues but this should work with either sync-request or request
// *******GET Request*******
var request = require('sync-request');
var res = request('GET', 'https://example.com', {
'headers': {
'user-agent': 'example-user-agent',
'authorization', 'Bearer ' + authId
}
});
// ****POST Request*****
var request = require('sync-request');
var res = request('POST', 'https://example.com/create-user', {
'headers': {
'authorization', 'Bearer ' + authId
},
json: { username: 'Name' }
});
authId needs to be whatever your bearer token spoils be for your app.
I would suggest use of axis and example below:-
GET
import axios from "axios";
axios({
method: 'get',
url: url,
headers: {
'Content-Type': 'application/json'
}
}).then(function (response) {
console.log(response);
}).catch((err) => {
console.log(err)
));
POST
axios({
method: 'post',
url: url,
data: JSON.stringify({orders}),
headers: {
'Content-Type': 'application/json',
'Authorization': userObj.token
}
}).then(function (response) {
console.log(response)
});
Where ubserObj.token -
Bearer Token ex: Bearer ASDF#!##!ADFASDF!##!##
This will be on the server side settings.
The server only accepts data sent in the headers.
Doing it using this code the server is getting empty object:
const request = require('request')
request.post({
url: 'https://.....',
body: { userid: 'cris', gameid: '12' },
headers: { "Content-Type": "application/x-www-form-urlencoded"}
})
Doing it with Postman, the server gets the correct data:
How can i use the code to send the data in the headers?
Edit:
A printscreen with server info displayed in the browser, should help.
Try this
const request = require('request')
request.post({
url: 'https://.....',
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json, text/plain', 'userid':'cris', 'gameid':'12'}
})
Try this
const request = require('request')
request.post({
url: 'https://.....',
body: JSON.stringify({ userid: 'cris', gameid: '12' }),
headers: { "Content-Type": "application/x-www-form-urlencoded"}
})
try this
body: { "userid": "cris", "gameid": "12" }