2021年5月2日星期日

Simple post working with request but not node-fetch

I'm trying to switch from the deprecated request package (I use request-promise-native on top of it) to node-fetch. But these two requests don't seem equivalent :

// node-fetch :    const result = await fetch(    `${process.env.PATEXTRACTOR_URL}/api/v2/graph`,    {      method: "post",      body: JSON.stringify(buildPatentExtractBody(selection)),      agent: new Agent({ keepAlive: true })    }  ).then(result => result.json());    // request-promise-native :    const { data } = await request.post({    url: `${process.env.PATEXTRACTOR_URL}/api/v2/graph`,    json: true,    body: buildPatentExtractBody(selection),    agentOptions: {      keepAlive: true    }  });    // Should be the same :    console.log(result);  console.log("________");  console.log(data);  

node-fetch fails with an error 400 (from the targeted API) which says "Missing parameters or request type not recognized") where request-promise-native succeeds and returns a nice json result.

I fail to see the difference between these two request. Any help ? Thank you.

https://stackoverflow.com/questions/67362732/simple-post-working-with-request-but-not-node-fetch May 03, 2021 at 10:05AM

没有评论:

发表评论