Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 725
Krisisonfire Avatar asked Feb 14 '17 09:02

Krisisonfire


People also ask

Can you use fetch for XML?

To execute a FetchXML query, you must first build the XML query string. After you create the query string, use the IOrganizationService. RetrieveMultiple method to execute the query string. The privileges of the logged on user affects the set of records returned.

Can fetch API be used for post?

It allows you to make an HTTP request, i.e., either a GET request (for getting data) or POST request (for posting data). The basic fetch request can be explained by the following code: 1 2 3 4 5 6 7 8 9 10 11 12 fetch('url') . then(response => { //handle response console.

How fetch API send data to form?

The Fetch API allows you to asynchronously request for a resource. Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

Is XMLHttpRequest better than fetch?

Fetch is a new native JavaScript API, supported by most browsers today. Fetch allows you to make network requests similar to XMLHttpRequest . According to Google Developers Documentation Fetch makes it easier to make asynchronous requests and handle responses better than with the older XMLHttpRequest .


1 Answers

Because you’re setting mode: 'no-cors' (why?…) for the request, browsers won’t allow you to set any request headers other than CORS-safelisted request-headers. See the spec requirements:

…if guard is "request-no-cors" and name/value is not a CORS-safelisted request-header, return.

Setting Content-Type: text/xml; charset=utf-8 makes it no longer a CORS-safelisted request-header; Content-Type is only a CORS-safelisted request-header if its value is application/x-www-form-urlencoded, multipart/form-data, or text/plain.

So the start of a solution here is to not use mode: 'no-cors'.

The only case where it usually makes sense to set mode: 'no-cors' is if you’re using Service Workers for caching responses—because for a mode: 'no-cors' request, browsers won’t let your script access any properties of the response, so the only useful thing you can do with it is cache it.

I'm trying to use Fetch API to handle POST of XML data to avoid cross-origin issues with XmlHttpRequest.

The Fetch API follows the same cross-origin request model as XHR, so it’s unclear what cross-origin issues you’d be attempting to avoid by using the Fetch API…

like image 150
sideshowbarker Avatar answered Sep 30 '22 06:09

sideshowbarker