2

I'm trying to use $resource to get datas from a REST web service.

here is my code :

    return $resource('http://serverURL', {}, {

                getQuartiers: {
                    method: 'GET',
                    headers:  {
                             'Authorization': 'Basic base64login:pseudostring',
                     },

                },


          });

The problem is that with headers like this, it's an option request that is send and i get an error 401 Unauthorized. If i remove headers, it's a GET request. But i need headers to send my Authorization string !

Otherwise the webservice is ok, either by going with chrome directly to the url => popup with login/pwd => correct datas at json format or by using postman chrome extension, which send a GET request.

What's the matter ?

3
  • your header request seems OK, I have used some thing like this in my web api project. I thing problem is with your web server and not with your JS codes. check your back end codes. Commented Dec 16, 2014 at 9:16
  • go check out Cors. w3.org/TR/cors. That's your problem atm. Its a preflight getting 401. Commented Dec 16, 2014 at 9:17
  • As using postman with an OPTION request works fine, i think it's not a server configuration problem !? Commented Dec 16, 2014 at 11:23

1 Answer 1

1

You don't seem to be handling the preflight Options requests.

Your Web API needs to respond to the Options request in order to confirm that it is indeed configured to support CORS.

To handle this, all you need to do is send an empty response back.

This extra check was added to ensure that old APIs that were designed to accept only GET and POST requests will not be exploited. Imagine sending a DELETE request to an API designed when this verb didn't exist. The outcome is unpredictable and the results might be dangerous.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not sureto understand...You talk about server config, isn't it ? Actually the server send response : Access-Control-Allow-Headers:Authorization Access-Control-Allow-Methods:GET Access-Control-Allow-Origin:* etc But it seems that my headers aren't correctly send :
Yes, that's on the server side, API configuration.
Usingpostman, the OPTION request header has this line : Authorization:Basic mystring Using my code, it is instead : Access-Control-Request-Headers:accept, authorization So i think my headers are not sent correctly !?

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.