6

I am using closure library to do a simple POST. I think XhrIo should work because from my machine when I use any other rest client ,like Firefox browser app RESTClient or Chrome's Simple Rest Client , I can make POST request to the server and content type is application/json.

But from my application I am unable to make a post. I am using the following code

xhr = new goog.net.XhrIo;
xhr.send('http://myhost:8181/customer/add','POST', goog.json.serialize(data));

If I leave the headers default, I get this

Encoding: UTF-8
Http-Method: POST
Content-Type: application/x-www-form-urlencoded;charset=UTF-8

If I try to change the header by passing {'content-type':'application/json'} as 4th parameter, header changes to

Http-Method: OPTIONS
Content-Type:

Shouldn't I be able to change headers appropriately with Closure library just as RESTClient does with XMLHttpRequest using JQuery ?

How else can the header be altered to make it appear like this

Encoding: UTF-8
Http-Method: POST
Content-Type: application/json;charset=UTF-8

Appreciate any help Eddie

Eddie
  • 125
  • 1
  • 2
  • 8

3 Answers3

9

When you add a header to an XHR object, most browsers will do a preflight request, which is the OPTIONS method that you are seeing. There is not a way to circumvent this if you are adding custom headers, unfortunately. The POST will be sent after the OPTIONS.

This article explains the OPTIONS request a bit. I ran into issues with the preflight a while back, if that is any help.

If you have specific issues with the OPTIONS request you should edit your question to include them; otherwise, this is expected behavior.

Community
  • 1
  • 1
Achal Dave
  • 4,079
  • 3
  • 26
  • 32
  • Thanks Achal , this was useful information. Just wonder how RESTClient (http://code.google.com/p/restclient/source/browse/extension/chrome/content/restclient.js) manage to do it ? Is it because they pass this passwordObject: null ? – Eddie Jul 07 '12 at 21:17
  • I don't really know, to be honest. What is the issue you have? Is the `OPTIONS` request getting denied? If so, what HTTP code are you receiving? – Achal Dave Jul 08 '12 at 22:38
  • @AchalDave the content length is coming 0 when POST is automatically getting converted to OPTIONS – 1Mayur Oct 08 '12 at 12:35
2

FWIW mine also failed to update the type when I specified...

{'content-type':'application/json'}

However, if I corrected the case to

{'Content-Type':'application/json'}

... it worked.

Go figure.

Rory Becker
  • 15,551
  • 16
  • 69
  • 94
0

If you are pass Content-Type on authorization request it will convert POST method to OPTIONS method so while we are use ouath and passing authorization token that time do not required Content-Type.

So do not pass Content-Type on all authorization request it won't change your method POST to OPTIONS

Parth Solanki
  • 3,268
  • 2
  • 22
  • 41
  • can you explain more about it? My method change to Options from POST when I'm posting a request to an API URL – jhon Jun 30 '20 at 03:37