【发布时间】:2021-08-30 17:27:29
【问题描述】:
我正在构建一个食品检测应用程序,并且我已经尝试了所有方法来消除这个错误!粘贴 url 时收到 400 bad request 错误。
这是显示错误的状态码:
> {"status":{"code":11102,"description":"Invalid request","details":"Empty or malformed authorization header. Please provide an API key or session token.","req_id":"7d399e162bb943f5848960862ccedbe9"}}
我的 App.js 是这个(没有包含导入部分和 api 密钥):
const app = new Clarifai.App({
apiKey: "MY API KEY",
});
const particleOptions = {
particles: {
number: {
value: 100,
density: {
enable: true,
value_area: 800,
},
},
},
};
class App extends Component {
constructor() {
super();
this.state = {
input: "",
imageUrl: "",
showModal: false,
foodIngredients: [],
};
}
onInputChange = (event) => {
this.setState({ input: event.target.value });
};
onButtonSubmit = () => {
this.setState({ imageUrl: this.state.input, showModal: true });
app.models
.predict(
{
model_id: " bd367be194cf45149e75f01d59f77ba7",
version_id: "dfebc169854e429086aceb8368662641",
},
this.state.input
)
.then((response) => {
console.log("hi", response);
if (response) {
fetch("http://localhost:3000/image/", {
method: "put",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
url: this.state.input,
}),
})
.then((response) => response.json())
.then((response) => {
//response ingredient array
if (response.length <= 0) {
return this.setState({
input: "",
foodIngredients: [response],
});
} else {
this.setState({
input: "",
foodIngredients: response,
});
}
});
}
})
.catch((err) => {
console.log(err);
this.setState({ imageUrl: "" });
});
};
onModalShow = () => {
this.setState({ showModal: false, foodIngredients: [] });
};
render() {
const { input, imageURL, showModal, foodIngredients } = this.state;
return (
<div className="App">
<Particles className="particles" params={particleOptions} />
<div>
<Logo />
<ImageLinkForm
urlInput={this.onInputChange}
text={input}
buttonClick={this.onButtonSubmit}
/>
<FoodRecognition
url={imageURL}
show={showModal}
canceled={this.onModalShow}
foodIngredients={foodIngredients}
/>
</div>
</div>
);
}
}
export default App;
如果我需要添加或更改任何内容,请告诉我。
更新 更改 .predict 时出现新错误
app.models
.predict(
Clarifai.FOOD_MODEL,
// {
// model_id: " bd367be194cf45149e75f01d59f77ba7",
// version_id: "dfebc169854e429086aceb8368662641",
// },
this.state.input
)
错误:
App.js:55 PUT http://localhost:3000/image/ 404 (Not Found)
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
【问题讨论】:
-
您显示的代码未显示对
https://api.clarifai.com/v2/models/searches的请求,这是错误的来源。请显示与实际错误对应的代码。 -
并且,在您的问题中包含代码作为文本(而不是图像),并正确格式化为代码。这是在 Stackoverflow 上发布问题的基准程序。由于各种原因,代码永远不应该出现在 Stackoverflow 上的图像中。它无法搜索。试图帮助的人无法复制/粘贴到答案中,外部图像习惯于随着时间的推移而消失,使得问题无法作为长期参考,等等......
-
抱歉,我已将问题(代码)编辑为文本。
-
当我在控制台中按下我的 app.js 时。它需要我去 .catch((err)。所以我只是粘贴了整个代码,认为它可以帮助人们理解我想要做什么。
-
您在代码中直接显示的唯一实际 HTTP 请求是与错误消息不对应的 localhost。错误来自
app.models.predict().catch(...)?那是错误显示的.catch()吗?如果是这样,那么我猜它来自 Clarifai 库内部,所以问题将出在您传递给它的参数中。
标签: node.js http-status-code-400 bad-request clarifai