Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

fetch failed from companion to web server

ANSWERED

Hi,

 

I made sideload app and I want to send my data(json type) to a web server.

 

But When I run the app, it simply prints "request failed" and no error message is displayed.

 

In the code below, I would like to ask you the wrong part or advice.

 

p.s. posted_data is json type data.

 

import * as messaging from "messaging";


// Listen for the onmessage event
messaging.peerSocket.onmessage = function(evt) {
  // Output the message to the console
  console.log("Received from device... "+JSON.stringify(evt.data));
  var posted_data = JSON.stringify(evt.data);
  
  console.log(posted_data);
  
  fetch("https://localhost:2002/index.html", {
    method: 'post',
    headers: {
      "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
    },
    body: posted_data
  })
  //.then(posted_data)
  .then(function (posted_data) {
    console.log('Request succeeded with JSON response', posted_data);
  })
  .catch(function (error) {
    console.log('Request failed', error);
  });
};

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Hard to say without seeing the server code.

 

Just start doing standard debugging to verify certain parts are working and work your way down to the point of failure.

View best answer in original post

Best Answer
5 REPLIES 5

try:

 

'Content-Type': 'application/json'

 

thats what I use after I stringified my JSON data like you did.

 

if that doesn't work you can try http request since it is on the localhost and you don't need https. This removes a source of error as maybe the authentication might fail. Also you can remove another source of error by using Postman to test your server for the FETCH API without the fitbit app. That way you can determine if its your fitbit code or your server code.

 

Also it may or may not be case sensitive:

 

method: 'post',

should be

method: 'POST',
Best Answer
0 Votes

Thank you for your reply!Smiley Happy

 

I tried to change code to 'Content-Type': 'application/json' and remove 'https:'.

 

And I re-execute my code but, I checked same error.. Smiley Sad

 

So, I changed my code more simply.

 

import * as messaging from "messaging";


// Listen for the onmessage event
messaging.peerSocket.onmessage = function(evt) {
  // Output the message to the console
  //console.log("Received from device... "+JSON.stringify(evt.data));
  var posted_data = JSON.stringify(evt.data);
  
  console.log(posted_data);
  
  const obj = {
  method: 'POST',
  headers: 'myHeaders',
  mode: 'cors',
  cache: 'default',
  body: posted_data
  }
  fetch('http://localhost:2002/index.html', obj)
  .then(res => res => res.json())
  .then(json => console.log(json))
  .catch(err => console.log(err));
};

Now, I get a error message

'TypeError: Failed to execute 'fetch' on 'Window': The provided value is not of type '(sequence<sequence<ByteString>> or record<ByteString, ByteString>)''.

 

In fact, it is the first time to create a web.

 

If I post to the url using the fetch function, how does it react to the index.html file of the web page I created?

 

 

 

 

Best Answer
0 Votes
headers: 'myHeaders',

you are assigning headers the string 'myHeaders' which means nothing to the fetch API. Take the quotes away.

 

This is my code. Give it a shot. Find what your local IP is for your server and replace it with mine.

 

    fetch('http://127.0.0.1:8100', {
      method: 'POST',
      headers:{'Content-Type': 'application/json'},
      body: myJSONData
    })
    .then(response => {console.log(response);})
    .catch(error => {console.log(error);})
    .then(response => {console.log(response);});

 

 

Best Answer
0 Votes

Thank you for your reply!Smiley Happy

 

I tried to your code writed below!

 

  fetch('http://10.100.28.120:8080', {
      method: 'POST',
      headers:{'Content-Type': 'application/json'},
      body: posted_data
    })
    .then(response => {"response: "+console.log(response);})
    .catch(error => {"error: "+console.log(error);})
    .then(response => {"response: "+console.log(response);});

But, I get error message "TypeError: Failed to fetch" and "undefined".

 

I thinkTypeError is caused by data receiving in the web rather than posting data from the fetch function.

 

I wonder what you think.

Best Answer
0 Votes

Hard to say without seeing the server code.

 

Just start doing standard debugging to verify certain parts are working and work your way down to the point of failure.

Best Answer