thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.62k stars 128 forks source link

Connection was refused by the server. #170

Closed berkobob closed 3 years ago

berkobob commented 3 years ago

Describe the bug I've written a very simple REST JSON server in Dart using Shelf. I can make requests via a browser or Postman but when using Thunder Client I get "Connection was refused by the server."

To Reproduce http://127.0.0.1:8081/ http://localhost:8081/

Expected behavior I would expect "200 Server running"

Platform:

Solution:

rangav commented 3 years ago

Hi @berkobob thanks for reporting the issue.

berkobob commented 3 years ago
Screenshot 2021-05-22 at 12 37 07

I'm not using a proxy and it's all http (no https)

On Sat, 22 May 2021 at 12:23, Ranga Vadhineni @.***> wrote:

Hi @berkobob https://github.com/berkobob thanks for reporting the issue.

  • Can you share a screenshot, with request and response.
  • Are you using any proxy?
  • Is the request redirecting http to https in browser?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rangav/thunder-client-support/issues/170#issuecomment-846394037, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSDRKZUFNYQRRA7H6F3RDLTO6HZZANCNFSM45KREYQQ .

rangav commented 3 years ago

Thanks @berkobob can you also share screenshot of headers tab, to see what headers are sent.

berkobob commented 3 years ago

[image: Screenshot 2021-05-23 at 09 10 18]

On Sun, 23 May 2021 at 00:03, Ranga Vadhineni @.***> wrote:

Thanks @berkobob https://github.com/berkobob can you also share screenshot of headers tab, to see what headers are sent.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rangav/thunder-client-support/issues/170#issuecomment-846473820, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSDRK3NVWAKCNO2H25POC3TPAZ2PANCNFSM45KREYQQ .

rangav commented 3 years ago

I think it could be issue with headers, as its working in browser.

Can you try below and see if its working or not.

  1. Remove content-type header and test it.

  2. please copy the url that is working in browser and import as curl into thunder client and test it. image

berkobob commented 3 years ago

Are we on the same version? I don't know what you pressed to get that menu? If you look at this screenshot, I was able to find "Import Curl" by pressing on the icon I've circled. I then entered the url which was successful in the browser.

Screenshot 2021-05-23 at 13 36 25

However, all this did was create your default query as you can see in this screenshot

Screenshot 2021-05-23 at 13 37 07

Also, if you look at the bottom in the terminal you will see a successful curl command

rangav commented 3 years ago

Open your localhost url - http://127.0.0.1:8081/ in the browser, and copy as cURL from network tab and import into Thunder Client.

image

berkobob commented 3 years ago

Found it! Thanks. So this is what was copied

curl 'http://localhost:8081/' \
  -H 'Connection: keep-alive' \
  -H 'Cache-Control: max-age=0' \
  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'Upgrade-Insecure-Requests: 1' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
  -H 'Sec-Fetch-Site: none' \
  -H 'Sec-Fetch-Mode: navigate' \
  -H 'Sec-Fetch-User: ?1' \
  -H 'Sec-Fetch-Dest: document' \
  -H 'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \
  --compressed

but no luck. Here is the screen shot after importing the curl command:

Screenshot 2021-05-23 at 14 52 07
rangav commented 3 years ago

is it working on the browser? can you share the browser screenshot showing that the url is working and postman screenshot

berkobob commented 3 years ago

Yes, it works on all my browsers, cURL, Postman etc. Here's a browser screenshot

Screenshot 2021-05-23 at 15 16 45
rangav commented 3 years ago

can you also share postman screenshot

Also stop your local server and restart again.

See some help links https://serverfault.com/a/590329/242340 https://stackoverflow.com/a/43345862/4035948

berkobob commented 3 years ago

I've restarted the server but it made no difference. Here is the Postman screenshot

Screenshot 2021-05-23 at 15 29 22
rangav commented 3 years ago

Cant find where is the problem. Anyway thanks for sharing the screenshots, if I find a solution, i will get back to you.

hygsani commented 3 years ago

same thing happened to me when i'm using PHP built-in server with this command => php -S localhost:8000 -t public/ after i changed the 'localhost' to '0.0.0.0' the message gone and the requests working

rangav commented 3 years ago

thats good to know. you finally found the solution :)

rangav commented 3 years ago

I am closing the issue, feel free to re-open if needed.

berkobob commented 3 years ago

That wasn't me nor does that solution work for me. Please re-open. Thank you.

rangav commented 3 years ago

@berkobob re-opened it, thought it was you.

hygsani commented 3 years ago

i haven't use dart, but i guess the problem is on 'localhost' try change the 'localhost' to '0.0.0.0' in dart server listener (shelf_io.serve(params))

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;

void main() async {
  var handler =
      const Pipeline().addMiddleware(logRequests()).addHandler(_echoRequest);

  var server = await shelf_io.serve(handler, '0.0.0.0', 8080);

  // Enable content compression
  server.autoCompress = true;

  print('Serving at http://${server.address.host}:${server.port}');
}

Response _echoRequest(Request request) =>
    Response.ok('Request for "${request.url}"');
rangav commented 3 years ago

also there are 2 other useful links on web related to this issue, see if any of them works?

https://github.com/dart-lang/http/issues/233 https://stackoverflow.com/questions/23691105/dart-servers-refusing-connection-in-local-network/23691467

berkobob commented 3 years ago

It worked!! Thanks so much. I just changed my hostname from localhost to 0.0.0.0 and it works perfectly. Thanks again.

rangav commented 3 years ago

Thanks @berkobob for confirmation

and Thanks to @hygsani for finding the solution.

JoelBonetR commented 3 years ago

Same here, any request to localhost:port ends up in a "Connection was refused by the server." error message.

I was setting up a Node Express server, without athentication nor authorization services in the middle, nor SSL, just http get request. If I run localhost:5000 on the browser I get the expected JSON.

I tried disabling the windows defender firewall just in case but nothing changed.

marlonbrum commented 2 years ago

Just to add something to the case, here I was using php builtin server , with "php -S localhost:8000 -t public", and was getting the same error. After I changed the command to "php -S 127.0.0.1:8000 -t public" it worked. It works when i call http://127.0.01/api" AND "http://localhost/api"

s850042002 commented 2 years ago

Here's my finding to those are using golang gin. I got the same error no matter I use router.Run(":8080") or router.Run("0.0.0.0:8080"). Only router.Run("127.0.0.1:8080") works. But I guess it's for local testing only.

EASTF0X commented 2 years ago

Hello everyone!

I had the same issue and none of the above mentioned solutions worked for me... I was close to desperation! 😄

Instead of using 'localhost' or your local ip address try : [::1] <-- yes use with brackets []! (of course this is also your local ip address, but yea... I don't understand why this worked for me but not 127.0.0.1 ... apparently my setup is ipv6 only ?)

image

fijiwebdesign commented 2 years ago

Hello everyone!

I had the same issue and none of the above mentioned solutions worked for me... I was close to desperation! 😄

Instead of using 'localhost' or your local ip address try : [::1] <-- yes use with brackets []! (of course this is also your local ip address, but yea... I don't understand why this worked for me but not 127.0.0.1 ... apparently my setup is ipv6 only ?)

image

It appears that using localhost in ThunderClient doesn't resolve correctly to the ipv6 address [::1] if the server is attached to that address.

The issue is on some machines using just a port to attach your server will attach to [::1] address automatically. The browsers will correctly resolve localhost to [::1] ThunderClient resolves localhost to 127.0.0.1

If we could see the ThunderClient source we could contribute a fix :)

Mitigations are to use ip4v address as your server host address.

For my example with fastify

const addr = await fastify.listen(PORT, '127.0.0.1')
    console.log('Server running at ' + addr)

with Express

app.listen(port, '127.0.0.1', () => {
  console.log(`Example app listening on port ${port}`)
})

This way you can use localhost in ThunderClient.

You can also use [::1] in the URL for Thunderclient direclty as @EASTF0X mentioned.

Example

http://[::1]:5050/api

Or better you could create an env var for host and make the value [::1] for local development


/*
Env: Local Dev
host [::1]

Env: Production
host myapi.com
*/

http://{{host}}:5050/api

That way you can switch the host based on enabled env (development, production etc.) without fiddling with localhost resolution.

rangav commented 2 years ago

Hi @fijiwebdesign

I am using below library for http requests https://github.com/sindresorhus/got

if you find any option to fix this, let me know.

rangav commented 2 years ago

they have dns lookup version to configure https://github.com/sindresorhus/got/blob/dbbd31777b3299ceb0db99798453e01ab8e597b1/documentation/2-options.md#dnslookupipversion

fijiwebdesign commented 2 years ago

@rangav got only supports one ip address at a time. So if there are multiple IPs you'd have to make multiple got() requests. That would mean each request would use up the request limit's before trying the next. You could set the request limit to 1 and try the next ip in succession but that would make the other configs for options.retry useless.

options.retry has extensive logic for retry timeouts, delay and backoff so best to keep it while trying each ip. https://github.com/sindresorhus/got/blob/dbbd31777b3299ceb0db99798453e01ab8e597b1/documentation/7-retry.md

The following will keep options.retry configs but rotate through the IPs returned from DNS while making only one dns request per got() request.


import got, { Options } from 'got'
import dns from 'dns'

async function fetchUrl(url, options) {
  let dnsEntries
  let fetchCount = 0
  const host = (new URL(url)).hostname
  const dnsLookup = (host, options, cb) => {
    if (dnsEntries) {
      fetchCount++
      const nextEntry = dnsEntries[fetchCount % (dnsEntries.length)]
      return cb(null, nextEntry.address, nextEntry.family)
    }
    options = {
      ...options,
      all: true
    }
    dns.lookup(host, options, (err, addresses) => {
      if (err) throw err
      dnsEntries = addresses
      if (!addresses?.length) throw new Error('DNS query failed')
      dnsLookup(host, options, cb)
    })
  }
  const res = new Promise(resolve => {
    dnsLookup(host, { all: true }, async (err, addresses) => {
      if (err) throw err
      const retryLimit = (options?.retry?.limit || 3) * addresses.length
      const res = await got(url, undefined, new Options({
        ...options,
        dnsLookup,
        retry: {
          ...options?.retry,
          ...{
            limit: retryLimit
          }
        }
      }))
      resolve(res)
    })
  })

  return res
}

fetchUrl('http://localhost:5050/trades')
  .then((res) => console.log(res.body))
  .catch(err => console.log('Error', err.message))

So if you did one got('localhost') and retry.limit=3 it would do.

dns request -> 2 ips [ipv4, ipv6]
got(ipv4) -> host fail 1
got(ipv6) -> network fail 1
retry got(ipv4) -> host fail 2
retry got(ipv6) -> network fail 2
retry got(ipv4) -> host fail 3 -> retry limit reached
retry got(ipv6) -> success on try 3
josephcozad commented 2 years ago

@rangav Hello. I have the exact same error. However I get this error not when accessing localhost or 0.0.0.0. I get this error trying to send a request to a server within our company's domain. Like the above, if I use a browser or Postman, no issue. The other thing that's odd is that I had this issue one day, then the next it was gone, then the next day it reappeared. I didn't do anything I can think of that would suggest a "clearing" of history/cache. Computer hasn't been restarted. I have tried closing and restarting VSCode but that hasn't helped.

Mostly I wanted to make sure that you guys knew that this isn't relegated to localhost. At lease for me I get the same issue trying to access any server on our company network with this plugin in VSCode and it's spotty. Also I have been using Thunder Client for over a year without issue. Only in the last couple of days has this appeared for me. My VSCode is version 1.66 and Thunder Client is v1.16.0. I'm on a Windows 10 OS.

rangav commented 2 years ago

@josephcozad, @fijiwebdesign thanks for the feedback, will research further on this issue and get back to you.

Kaviah commented 2 years ago

Hi, I´m having the same issue but I couldn´t solve with your solution, can you help me to find out if I did right? Thank you! erro-thunder-erro erro-thunder-index

rangav commented 2 years ago

@Kaviah did you try http://localhost:3000/users?

Alynva commented 2 years ago

I think that this is still an open issue. For me, it worked using ipv6 [::1] as host on Thunder, but having to explain this problem to every coworker its not great. The usage of Thunder shouldn't force me to change my servers running in localhost to run on 127.0.0.1 or something else... I know that the source problem may come from got, but the message Connection was refused by the server. (that isn't clear at all) is a Thunder issue and it is still happening...

rangav commented 2 years ago

@Alynva thanks for reporting.

got library is causing lot of issues around the connection refused, might move to axios.

if you are using node for development. can you confirm your localhost address is accessible using with axios library without any issues?

Alynva commented 2 years ago

@Alynva thanks for reporting.

got library is causing lot of issues around the connection refused, might move to axios.

if you are using node for development. can you confirm your localhost address is accessible using with axios library without any issues?

I'll try to create an reproducible repo comparing got and axios behavior, but I can't promise it will be right now

rangav commented 2 years ago

@Alynva simple url access test is enough just to confirm its working or not.

ok let know when you done the testing with axios.

rangav commented 2 years ago

Hi all, we add new http library option for axios in v1.18.3. please update test it and let me know your feedback

http

See features released https://github.com/rangav/thunder-client-support/releases/tag/v1.18.3

vifeng commented 1 year ago

http library option for axios is not working on my end. best option is [::1]. tell me if you need me to provide something and what

Kaviah commented 1 year ago

I've noticed my lack of attention to that little detail... Sorry about that... And thank you for your reply!

Sandra Ferla +55(75)99213-4011 www.kaviah.com canal no Telegram https://t.me/kaviah_com

Em sáb., 9 de jul. de 2022 às 11:33, Ranga Vadhineni < @.***> escreveu:

@Kaviah https://github.com/Kaviah did you try http://localhost:3000/users?

— Reply to this email directly, view it on GitHub https://github.com/rangav/thunder-client-support/issues/170#issuecomment-1179554461, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGUASICMRNTCC5SNWAW2TD3VTGESHANCNFSM45KREYQQ . You are receiving this because you were mentioned.Message ID: @.***>

belachkar commented 1 year ago

Resolved by changing the backend server address on the application: From localhost to 127.0.0.1 or 0.0.0.0.

// Web Server
const PORT = process.env.PORT || 3001;
const HOSTNAME = '0.0.0.0';
marckregio commented 1 year ago

Resolved on my end by changing http://localhost:3000 to [::]:3000. I am just curious why it has to be like this. Screenshot 2022-11-20 at 11 10 12 AM

rangav commented 1 year ago

@marckregio it is related to ipv6 address

Read this https://stackoverflow.com/a/40189197/4035948

hungtcs commented 1 year ago

Sorry I found the reason, all the issues are cause by my vscode proxy settings.


Hello, I have the same problem, I use nestjs created a server and listen host 0.0.0.0. Then copy curl from browser devtool and import into thunder-client.

I tried ipv6 still not working.

curl 'http://127.0.0.1:3001/swagger-json' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Cookie: AGL_USER_ID=fd053bc9-afdc-4702-a5ac-0620bc315fd5; Hm_lvt_a8b32306ce37093997d8923fa6bcda22=1662542692; login_region=; login_domain=; csrftoken=yLcM6XgrDSveOC5oO3HNyfXcKJucP495; Hm_lvt_a0ff3b9a602190befef9ba3befd37f57=1667290576,1668656041,1669261615' \
  -H 'DNT: 1' \
  -H 'Pragma: no-cache' \
  -H 'Sec-Fetch-Dest: document' \
  -H 'Sec-Fetch-Mode: navigate' \
  -H 'Sec-Fetch-Site: none' \
  -H 'Sec-Fetch-User: ?1' \
  -H 'Upgrade-Insecure-Requests: 1' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --compressed

image

Test the curl command in terminal is working well:

image

steelvelveteen commented 1 year ago

Hi all, tried all the solutions provided here. My Thunder Client just stopped working. I had to download Postman to test it works just fine on Postman. Neither alternatives to localhost have worked for me. Please keep issue open

rangav commented 1 year ago

@steelvelveteen can you please screenshot of the request and error

steelvelveteen commented 1 year ago

image

steelvelveteen commented 1 year ago

And my headers image

rangav commented 1 year ago

@steelvelveteen thanks for sharing screenshots.

Did you try [::1]:56752 or localhost:56752?

Please try below solutions

Option 1:

try localhost:56752 and enbale preview features settings

image

Option 2:

Change http library to axios in settings image