5

I am trying to send a POST request with body as form-data since this seems to be the only way that works.

I tried this in Postman too and sending body as raw JSON didn't work.

So I tried doing the same with node-fetch but seems like body is being sent as JSON and I'm getting the same error as before (when using raw from Postman).

try{
  const { streamId } = request.body;
  const headers = {        
    "Authorization": INO_AUTHORIZATION_CODE,
    // "Content-Type": "multipart/form-data; boundary=<calculated when request is sent>"
    "Content-Type": "application/json"
  }      
  const url = `https://www.inoreader.com/reader/api/0/stream/contents/${streamId}`;
  const body = {
      AppId: INO_APP_ID,
      AppKey: INO_APP_KEY
  }
  
  const resp = await fetch(url, {
      method: 'POST',
      body: JSON.stringify(body),
    //   body: body,
      headers: headers
    });   
    
  const json = await resp.text();
  return response.send(json);
} catch(error) {
    next(error);
}

Only setting body as form-data works:

enter image description here

Azima
  • 3,835
  • 15
  • 49
  • 95

2 Answers2

9

You need to use the form-data package as mentioned in their doc so your code will be

const FormData = require('form-data');

const form = new FormData();
form.append('AppId', INO_APP_ID);
form.append('AppKey', INO_APP_KEY);

const resp = await fetch(url, {
      method: 'POST',
      body: form
    }); 
salman
  • 264
  • 1
  • 9
1

Usar:

const form = new URLSearchParams();
form.append('AppId', INO_APP_ID);
form.append('AppKey', INO_APP_KEY);
  • A good answer always includes some explanation to elaborate how it addresses the problem asked in question. – Zain Ul Abidin Nov 19 '22 at 05:47
  • Why do you use URLSearchParams for form data creation? As here(https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) said, URLSearchParams is defines utility methods to work with the query string of a URL. It's for working with search parameters not for form data. Please consider your answer. – Temuujin Dev Nov 25 '22 at 18:32
  • @TemuujinDev it says in their docs (the link that is in the other answer) to use `URLSearchParams`, and it never says to use the `form-data` package so this should be the answer marked as correct for other people that have this question – LuisAFK Dec 31 '22 at 12:51