vkurko/calendar

Unable to pass "headers" in EventSource POST

oldmanwerther opened this issue · 3 comments

When configuring to POST into my endpoint, it seems as if I can't add headers as a nested object in extraParams. The rendered payload shows as [object Object].

my js:

...
        eventSources: [
            {
                url: "https://localhost:44336/GetProducts",
                method: "POST",
                extraParams: {
                    mode: "cors",
                    referrerPolicy: "no-referrer",
                    headers: {
                        "Content-Type": "application/json",
                        "Accept": 'application/json'
                    },
                    body: bodyRequest
                }
            }
        ],...

The extraParams option is intended only for additional parameters, not headers. Unfortunately there is no option for headers, but you can implement your own HTTP request via eventSource as a function.

Yes, that worked. Thanks. Posting my modified code for anyone else:

eventSources: [
         {
             events: function (fetchInfo, successCallback, failureCallback) { 

                 fetch(apiEndpointUrl,
                     {
                         method: "POST",
                         mode: "cors",
                         headers: {
                             "Content-Type": "application/json",
                             "Access-Control-Allow-Origin": "*"
                         },
                         referrerPolicy: "no-referrer",
                         body: bodyRequest
                     }).then(res => {
                         return res.json();
                     }).then(res => {
                         successCallback(res);
                     });
             }

         }
     ]
 

Great!