Wednesday, February 22, 2017

How to send Cookie in Request header

I did one file upload with Form data by using fetch. I tried to send Cookie and content type as multipart/form-data. But its not taking these two attributes in header. Finally I solved it through ajax as well as fetch.

1. If you want to send cookie you must add credentials like below.

fetch('/users', {
  credentials: 'same-origin'
})

Suppose if its a cross origin request, credentials should be like below.

fetch('https://xxxxxxxx.com:4321/users', {
  credentials: 'include'
})

and Finally you dont want to add content type as multipart/form-data. Your request will add this automatically.

2. Suppose if you are using ajax request,  your code should be like below.

$.ajax({
       url : 'xxxxxxxxxxxxxxxxxxxxx',
       type : 'POST',
       data : formData,     // form data appended values
       processData: false,  // tell jQuery not to process the data
       contentType: false,  // tell jQuery not to set contentType -  It will set multipart by request automatically
       success : function(data) {
           console.log(data);
           alert(data);
       }
});


Check Internet explorer and open Network tab (F12) request header. Now all the header attributes were added perfectly.

Chrome may not show cookies in network tab, check Application tab, there cookies will be stored.


Thursday, February 16, 2017

Chrome throwing 302 Error and Working fine in Internet Explorer

Ago some days back, I got an issue in chrome browser for one service which is returning a status code error 302. I checked the same in REST Client and this also returning error code 302.

But the same service working fine with IE browser. Initially I tried lot of tried with setting header filed cache etc., But nothing worked.

What are the possibilities for 302 status code error and How to handle it ?

The below are some of the possibilities only. But mostly it will be involved within this.

1. There is redirect issue. possibility, you might have used sendRedirect method instead of forward in java.

2. If you have load balancing then definitely there redirect will happen. The way of handling redirect may produce 302 issue.

3. Chrome throwing 302 exception since it have additional security than IE.

4. Check REST client (Chrome web store) older version, it wont produce this 302 error. But latest Rest client will produce 302 error.

5. Firewall also may produce 302 error, but in this case IE also wont work.

6. If you are using client side request through fetch, you add credential= same-origin. It will solve this issue. It will cookies for your request.

7. Finally , You have an option to play around with the below important attributes.
 Access-Control-Allow-Origin : http://www.xxxxxxx.com
Access-Control-Allow-Credentials : true