简体   繁体   中英

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. 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.

However, based on your error message, I've determined you're trying to leverage the Lambda Face Recognition and Face Detection API via RapidAPI . 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. 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.

Instead, modify your request to use the files parameter (type binary ) to upload the raw binary data for your image(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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