mardi 14 février 2017

Using Fetch API to POST XML

I'm trying to use Fetch API to handle POST of XML data to avoid cross-origin issues with XmlHttpRequest. The issue I face is that despite setting my Content-Type to 'text/xml' (which is the only supported content-type header in this case) my request's content-type is being reset to text/plain resulting in a HTTP status of 415 from the requested content.

Here is my fetch function:

    function doFetch(Content)
    {
        return fetch(
        URL, { method: 'POST',
           mode: 'no-cors',
           headers: new Headers(
           {'Content-Type': 'text/xml; charset=utf-8',
            'Accept': '*/*',
            'Accept-Language': 'en-GB',
            'Accept-Encoding': 'gzip, deflate',
            'Connection': 'Keep-alive',
            'Content-Length': Content.length                
            }),
            body: Content
           });
    }

Content is a string of XML data.

And this is the header which is actually being used:

    Content-Length:1537
    content-type:text/plain;charset=UTF-8

Just wondering if what I'm trying to do is possible, and if it is what I'm doing wrong? (I'm kind of new to web-development).

Thanks!




Aucun commentaire:

Enregistrer un commentaire