简体   繁体   English

使用 localhost 向 API 发送 POST 请求

[英]Sending a POST request to API with localhost

I am sending an image to an API using my localhost address but for some reason it doesn't identify the image.我正在使用我的本地主机地址向 API 发送图像,但由于某种原因它无法识别图像。 It works fine if I use links on google.如果我在谷歌上使用链接,它工作正常。 The code looks something like this:代码看起来像这样:

unirest.post(requestString)
   .header("X-RapidAPI-Key", API_KEY)
   .field("urls", "http://localhost:4000/uploads/1570544614486-test.jpg")
   .field("album", ALBUM_NAME)
   .field("albumkey", ALBUM_KEY)
   .field("entryid", entryId)
   .end(result => {
       console.log(result.body);
});

I believe this will work once on a domain but I need it to work now for testing.我相信这将在域上工作一次,但我现在需要它来进行测试。 How can I make this work using my localhost?如何使用我的本地主机完成这项工作?

You haven't exactly specified what API you're reaching out to, so your mileage may vary with different APIs.您尚未准确指定您要联系的 API,因此您的里程可能会因不同的 API 而异。

However, based on your error message, I've determined you're trying to leverage the Lambda Face Recognition and Face Detection API via RapidAPI .但是,根据您的错误消息,我确定您正在尝试通过 RapidAPI 使用 Lambda 人脸识别和人脸检测 API This (linked) docs for this web service clearly show that the urls parameter you're attempting to use with your localhost URL above is actually meant to hold a comma-separated set of URLs to publicly-accessible image files.此 web 服务的(链接)文档清楚地表明,您尝试与上面的localhost URL 一起使用的urls参数实际上是为了保存一组以逗号分隔的可公开访问的图像文件的 URL。 The remote API can't possibly resolve localhost in this context, because (a) it can't possibly have any idea what IP localhost should refer to, and (b) it's highly likely that your localhost here doesn't respond to HTTP requests from the broader Internet.远程 API 不可能在这种情况下解析localhost ,因为(a)它不可能知道 IP localhost应该指什么,并且(b)你的localhost很可能不响应 Z293CDC866ZEA246FF9989EA246FF998 请求26来自更广泛的互联网。

Instead, modify your request to use the files parameter (type binary ) to upload the raw binary data for your image(s).相反,修改您的请求以使用files参数(类型binary )为您的图像上传原始二进制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM