0

I am trying to make an Ajax POST request to an image recognition API called Cloudsight using Jquery. So far my code is:

$.ajax({
  type: "POST",
  url: "http://api.cloudsightapi.com/image_requests",
  Authorization: "CloudSight [key]",
  data: {
    remote_image_url: "https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
    locale: "en-US"
  },
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

When I try to run it I get the Error: 400 (Bad Request) What am I doing wrong? As far as I can see the code seems to be alright...

6
  • I got: {error: "Non OAuth request received"} Commented Oct 19, 2015 at 16:22
  • Have you verified with the cloudsight API documentation that you are providing all the necessary fields? I can't seem to access it so I can't be sure on my own. Commented Oct 19, 2015 at 16:22
  • There could be an issue with the URL. Commented Oct 19, 2015 at 16:24
  • According to their documention: cloudsight.readme.io you must supply an image and a locale which I have done. The URL is just the google logo from the new tab page; I was using it as a test. They have a demo on their website which I just tested, and it doesn't seem to be working so their service may be down. However, I don't think I'd be getting this error if it was so I think something is wrong with the code? I took Bad Request to mean I had formed the request wrong. Commented Oct 19, 2015 at 16:28
  • 1
    P.S. That's not your real API key, right? Commented Oct 19, 2015 at 16:35

2 Answers 2

1

Have you tried something like this?

beforeSend: function (req){
    req.setRequestHeader("Authorization", "CloudSight [key]");
},
Sign up to request clarification or add additional context in comments.

3 Comments

When I do that with the colon, it gives me a syntax error, but if I add a comma instead (is that the correct syntax?) it gives me the same error as before...
It is indeed a comma. I'll fix it.
Good to know, but I'm still getting the 400 Bad Request error. Any other ideas? :/
0

In case anyone else looks at this, managed to solve the problem with this code:

$.ajax({
  method: "POST",
  url: "https://api.cloudsightapi.com/image_requests",
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Authorization", "CloudSight [key]");
  },
  data: {
    "image_request[remote_image_url]": "https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
    "image_request[locale]": "en-US"
  },
  success: function(msg) {
    console.log("It worked! :D Good POST request.");
    console.log(msg);
    console.log(msg.token);
    console.log(msg.url);
    console.log(msg.responseText);

    token = msg.token;
  },
  error: function(msg) {
    console.log("Sorry...");
    console.log(msg);
    console.log(msg.responseText);
  }
});

Thanks to Mario Cezar for his help with the authorization!

1 Comment

You should accept your answer, not mine. Though mine helped you, it didn't provided you with the full answer to your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.