[SOLVED] node proxy to asp.net mvc app payload is null


This Content is from Stack Overflow. Question asked by Raif

I’m trying to create a node proxy that takes requests from a web app and sends them along to an old asp.net mvc app in c# 4.6.

I’m using the npm package http-proxy. I’m pretty deep in it and understand pretty much exactly what it’s doing. I can see the request that is about to be sent and I can debug the c# app if the app receives the request.

If my request comes through as content-type = text, then mvc app receives the request, but in the action the payload is null. i.e. there are none of the values sent in the payload available in the input viewmodel.

If my request is content-type = application/json ( which is what asp.net mvc app front end sends ) then the mvc action break point is never hit and the request hangs.

I can see in dev tools what the original frontend is sending, and I can console log in the node app what it is about to send. When I view the request in node, there is just a ton of information, what shows up in the dev tools seems to be just the req headers and payload. I have compared those between the two and there are some minor difference ( host, origin, referer, and maybe some other minor stuff, I’ll post headers below ).

Lastly I can curl the end point successfully and receive a success message from mvc like so
curl -H "Content-Type: application/json" -X POST -d '{"userName":"asdf", "password":"asdf"}' http://mydomain/login/login

Here are the headers from the mvc frontend request

Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 252
Content-Type: application/json; charset=UTF-8
Host: example.net
Origin: http://example.net
Referer: http://example.net/
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36
X-Requested-With: XMLHttpRequest

These are from my new frontend

Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 15
Content-Type: text/plain;charset=UTF-8
Host: localhost:3002
Origin: http://localhost:3001
Referer: http://localhost:3001/
sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Linux"
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

And these are from my proxy about to submit to the mvc app

  accept: '*/*',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'en-US,en;q=0.9'
  connection: 'keep-alive',
  'content-length': '15',
  'content-type': 'text/plain;charset=UTF-8',
  host: 'localhost:3002',
  origin: 'http://localhost:3001',
  referer: 'http://localhost:3001/',
    'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '"Linux"',
  'sec-ch-ua': '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
  'sec-fetch-dest': 'empty',
  'sec-fetch-mode': 'cors',
  'sec-fetch-site': 'same-site',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36',

When I put a listener for “data” on the request I do see the payload going out. Obviously there is a ton more going on in request, I just don’t know what is relevant and what is not. I will say that the urls are the same :).

Any help would be greatly appreciated and please let me know what else I can post to help clarify.


So I’m answering my own question, actually someone with the handle geirha on irc helped me.

I had koa-bodyparser in the middleware ( for some reason ) and while I can’t see why, when I used that in conjunction with setting the content-type="application/json" the request just hung on the asp.net mvc server. So without the bodyparser and with the content-type it did work.

I swear you try all these permutations thinking you’ve tried them all and there’s always that slips by and that ultimately is the one you needed.

This Question was asked in StackOverflow by Raif and Answered by Raif It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?