janhommes / o.js

o.js - client side oData lib.
https://janhommes.github.io/o.js/example/
MIT License
238 stars 57 forks source link

o.js autoFormat setting to false not working #66

Closed be236 closed 6 years ago

be236 commented 6 years ago

When I define this:

o().config({autoFormat: false}); <-- so I don't want o.js to attach ?$format=json at end of my URL

And make my OData GET call, it returns with a 406 HTTP error:

o.js exception: Request to https://MY_URL/mgmt/odata/tm/ltm/pool failed with HTTP status 406.

Yet when I make a curl call to my URL (as shown above), it works fine to return my resources, such as:

curl -s -k -u user:password -H 'Content-Type: application/json' -X GET https://MY_URL/mgmt/odata/tm/ltm/pool

Can someone confirm setting that field to false works or fails. If I set it to true, then it does attach the "$format" at the end of my URL and my o.js call works.

janhommes commented 6 years ago

So you are using basic auth? Did you configure username and password?

o.js itself is setting the following request header: 'Accept', value: 'application/json,text/plain'

Based on the status code your endpoint seems not to accept that.

be236 commented 6 years ago

Yes, I am using basic auth, here's my o() config:

o().config({ username:USER, password:PASS, strictMode: true, headers:[{name:'Prefer',value:'odata.maxpagesize=25'},{name:"OData-MaxVersion",value:"4.0"}], version:4 });

I didnt realize o.js sets this in its request header:

'Accept', value: 'application/json,text/plain'

How would I know this? is there a debug to display the header o.js attaches for me? Is there a way to override that header or suppress o.js from doing that for me?

janhommes commented 6 years ago

The goal of this lib was to make consuming odata in javascript as easy as possible. Therefore we force json (text/plain was added for $count). You can, however, add any accept type by setting the custom headers. A way to remove them is at the moment not implemented.

You can always see the headers by using the debug tools from Google-Chrome. The Network-Tab shows you exactly which headers are sent.

be236 commented 6 years ago

The explanations here have resolved this issue. Thank you. I was able to make changes needed to get my script to work.