sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.31k stars 936 forks source link

Post request to API returns response code 500 (Internal Server Error) whereas CURL returns 200 status code #2331

Open marketier opened 9 months ago

marketier commented 9 months ago

Post request to API returns Response code 500 (Internal Server Error)

I have an API written in C# .NET. One of the endpoints allows users to unsubscribe from emails and does not return any json response.

When using CURL or Postman, the API returns a 200 status code. GOT returns a 500 server error. On the web server itself, I see the following in logs:

Line 105662: 2024-02-15 09:26:14 10.52.1.9 POST /Tapier/api/TapierSubsciption/Subscribe isUnsubscribe=true&email_id=someemailid&healthcheck=1 443 - 10.52.1.211 got+(https://github.com/sindresorhus/got) - 500 0 0 307
Line 200535: 2024-02-15 16:40:19 10.52.1.9 POST /Tapier/api/TapierSubsciption/Subscribe isUnsubscribe=true&email_id=someemailid&healthcheck=1 443 - 10.52.1.211 PostmanRuntime/7.36.1 - 200 0 0 985

I am unable to upgrade to a more recent version of nodejs as the host is outside of our control.

I can confirm the issue also occurs on Nodejs 18 and Nodejs 20 in got@14.2.0.

Actual behavior

The following error is reported after running the script:

HTTPError: Response code 500 (Internal Server Error)
    at Request.<anonymous> (file:///home/vagrant/samplenodejs/node_modules/got/dist/source/as-promise/index.js:86:42)
    at Object.onceWrapper (node:events:628:26)
    at Request.emit (node:events:525:35)
    at Request._onResponseBase (file:///home/vagrant/samplenodejs/node_modules/got/dist/source/core/index.js:604:22)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Request._onResponse (file:///home/vagrant/samplenodejs/node_modules/got/dist/source/core/index.js:646:13) {
  input: undefined

Expected behavior

Curl and Postman both return 200 status codes. Expecting the same from GOT.

Code to reproduce

The API endpoint in the sample is an non-working example but the rest of the code is valid.

import got from 'got';

const url = 'https://api.domain.my/TapierService/api/TapierSubsciption/Subscribe?isUnsubscribe=true&email_id=someemailid&healthcheck=1';

const options = {
        body: '[{"subscription_url": "","event": ""}]',
        headers: {
          'Content-Type': 'application/json'
        },
};

const data = await got.post(url, options);
...
marketier commented 9 months ago

Any thoughts on the above case? What's the best way of troubleshooting or gathering more details about what GOT is attempting to post? Thanks.

masontikhonov commented 8 months ago

Hi @marketier, I don't think it's an issue with Got itself, but with your API server: 500 status code reflects an issue on server side.

The best way you could investigate it is to check API server debug-level logs while doing requests from Got. Hopefully, those logs will contain more information about the reasons why it can't process requests received from Got.

If you don't have an access to the server logs, or they are not descriptive enough, you could go other way:

  1. Write down all request properties that Got has sent to the server (i.e. headers, search params, etc)
  2. Send a request with the same params from Postman/CURL and check if it fails as well (it apparently will)
  3. By removing the properties one-by-one, find that one which causes the error.

For example, from your server logs it's clear that User-Agent header sent by Postman is set to PostmanRuntime/7.36.1, but the same header sent by Got is set to got+(https://github.com/sindresorhus/got) — try staring with this information, maybe even this particular header causes some issues on API server side.