admin管理员组文章数量:1421689
EDIT: Solved, posted details below as answer... Pretty stupid user error
I'm trying to write simple plugin for SteelSeries Gamesense to display currently playing song from iTunes in GameDAC screen. Basically the engine works via provided server listening to post requests with JSON body. I've been trying to get my request working for quite a while but with no go.
I tested request on Postman and it should be working as intended, so the problem is somewhere in syntax probably.
const axios = require('axios');
const coreProps = require(process.env.ProgramData + '/SteelSeries/SteelSeries Engine 3/coreProps.json');
const url = JSON.stringify(coreProps['address']);
axios.defaults.baseURL = 'http://' + url.replace(/"/g,'');
axios.defaults.headers['post'] = {'Content-Type': 'application/json'};
console.log(axios.defaults.headers);
function bind_itunes() {
const data = {
"game": "ITUNES",
"event": "NOWPLAYING",
"handlers": [
{
"device-type": "screened",
"zone": "one",
"mode": "screen",
"datas": [
{
"has-text": true,
"context-frame-key": "songname"
}
]
}
]
};
axios.post('game_event', JSON.stringify(data))
.then((res) => {
console.log(res)
}).catch((error) => {
console.error(error)
})
}
bind_itunes();
Code fails with long error block from Axios with error
"data: { error: 'passed value not string or JSON object' } }"
full error log (pastebin since it's quite long):
Postman screenshot
EDIT: Solved, posted details below as answer... Pretty stupid user error
I'm trying to write simple plugin for SteelSeries Gamesense to display currently playing song from iTunes in GameDAC screen. Basically the engine works via provided server listening to post requests with JSON body. I've been trying to get my request working for quite a while but with no go.
I tested request on Postman and it should be working as intended, so the problem is somewhere in syntax probably.
const axios = require('axios');
const coreProps = require(process.env.ProgramData + '/SteelSeries/SteelSeries Engine 3/coreProps.json');
const url = JSON.stringify(coreProps['address']);
axios.defaults.baseURL = 'http://' + url.replace(/"/g,'');
axios.defaults.headers['post'] = {'Content-Type': 'application/json'};
console.log(axios.defaults.headers);
function bind_itunes() {
const data = {
"game": "ITUNES",
"event": "NOWPLAYING",
"handlers": [
{
"device-type": "screened",
"zone": "one",
"mode": "screen",
"datas": [
{
"has-text": true,
"context-frame-key": "songname"
}
]
}
]
};
axios.post('game_event', JSON.stringify(data))
.then((res) => {
console.log(res)
}).catch((error) => {
console.error(error)
})
}
bind_itunes();
Code fails with long error block from Axios with error
"data: { error: 'passed value not string or JSON object' } }"
full error log (pastebin since it's quite long): https://pastebin./aLguKQ2C
Postman screenshot
Share Improve this question edited Jun 7, 2019 at 2:23 Janne Löfhjelm asked Jun 7, 2019 at 1:30 Janne LöfhjelmJanne Löfhjelm 711 silver badge5 bronze badges 5-
Why are you using
JSON.stringify()
on a URL? Can you share some screenshots of it working in Postman? – Phil Commented Jun 7, 2019 at 1:38 -
FYI, the Axios default content type is already JSON. You can just use
axios.post('game_event', data)
– Phil Commented Jun 7, 2019 at 1:42 - Forgot to mention @Phil, got same problem without stringify() and decided to add it there. Postman screenshot: imgur./9gEef9n . The underscore shouldn't matter since it's only chosen identifier for future when there will be text sent via that event bound to Engine. Resorting to default headers also has same result – Janne Löfhjelm Commented Jun 7, 2019 at 1:47
-
1
Postman is posting to
bind_game_event
but your code is posting togame_event
so they're definitely not the same – Phil Commented Jun 7, 2019 at 2:21 - Yah... Noticed myself a second ago. Thanks for answer anyways, next time I'll make extra steps to eliminate typos like this. Feel free to post as answer so I can flag as solved. – Janne Löfhjelm Commented Jun 7, 2019 at 2:25
2 Answers
Reset to default 3Next time before asking question, I'll also make sure to triple check the API endpoints.
As seen when paring the screenshot and code, I'm polling on wrong endpoint (game_event instead of bind_game_event), which quite obviously causes request to be bad.
Fixed the problem after hours and hours of wondering.
Thanks for assistance to everybody who tried and sorry to bother.
I would have suggested the same as @Phil: not to stringify your payload when using axios.post
. The examples in the documentation of Axios might be useful: https://github./axios/axios. I took a look at your screenshot, it seems that you got a successful response with a status code of 200. Are you still having an issue or is the response to your request different?
版权声明:本文标题:javascript - Problem with using Axios to post JSON ('passed value not string or JSON object') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745355719a2655038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论