C
C#this hour
b

✅ Sending an HTTP GET request with a JSON body and then reading it in the .NET Core Web Server

Stack Overflow
How can I send a GET request with JSON body?
Is there any way at all that I can send a GET request with a JSON body using C#? I am making a call to an API to retrieve a list of items using a GET request, but I have to pass the customer_id in ...
Stack Overflow
How to use HttpClient to send content in body of GET request?
Currently to send a parameterized GET request to an API interface I am writing the following code: api/master/city/filter?cityid=1&citycode='ny' But I see that there is a limit on the URL leng...
49 Replies
Keswiik
Keswiik23h ago
Are you getting any errors? What issues are you having?
cap5lut
cap5lut22h ago
as a side note: what u are doing is essentially UB
The GET HTTP method requests a representation of the specified resource. Requests using GET should only be used to request data and shouldn't contain a body.
Note: The semantics of sending a message body in GET requests are undefined. Some servers may reject the request with a 4XX client error response.
(source) so if u have control over both the client and the server/endpoint u should design it in another way
b
bOP21h ago
i do have control over it but this is for school and I have to do it like this. yes its not how u should but thats the point my prof wants me to figure out how to make it work.
cap5lut
cap5lut21h ago
then ⤴️ is the important stuff
b
bOP21h ago
Just doesnt work (404 just like how @cap5lut described it would be). the server seems to be rejecting the request nothing is going through.
cap5lut
cap5lut21h ago
by ".NET Core Web Server" i assume ASP.NET Core?
b
bOP21h ago
yes
cap5lut
cap5lut21h ago
and no inbetween web server that works as reverse proxy like IIS, nginx, etc?
b
bOP21h ago
sorry i dont understand? I am launching the web server through IIS express if thats what u mean?
cap5lut
cap5lut21h ago
so basically u just start u asp.net core web api/web app project to get the server to run right?
b
bOP21h ago
yes
cap5lut
cap5lut21h ago
well, u run it through iis, that could be already be an issue
b
bOP21h ago
how so
cap5lut
cap5lut21h ago
a reverse proxy basically delegates incoming requests to asp.net core, now u have 2 points where the request could be stopped
b
bOP21h ago
i do have other endpoints that use get/post/delete etc and they all work btw tbh i dont even know what that means or atleast not enough to debug and figure out where the issue is happening how would I go about figuring it out?
cap5lut
cap5lut21h ago
think of it as a man in the middle, the client talks to the iis express, the iis express talks to the asp.net core application, etc
b
bOP21h ago
how would i be able to figure out if its the application or the iis express that is at fault?
cap5lut
cap5lut21h ago
do u see in the logs the incoming request? (the easiest would probably be to start the asp.net core project as stand alone process without iis express)
b
bOP21h ago
sorry i really dont understand. ive just been using postman and curl command to send requests so far with iis express being how it is getting launched
cap5lut
cap5lut21h ago
i assume u r using visual studio and i cant check there how to start it that way, because i am using jetbrains rider. but essentially u could open the terminal, go to the project folder and execute dotnet run and it should start as standalone process without iis express
b
bOP21h ago
ok holdup let me try that
cap5lut
cap5lut21h ago
you probably will have to set up HTTP logging
b
bOP21h ago
ahh ok
b
bOP21h ago
but yeah when i do that im getting smth like this
No description
cap5lut
cap5lut21h ago
yeah thats the normal logging ur application does these logs look fine
b
bOP21h ago
I was just using console writeline inside the endpoints to see if its getting reached for now but it wasnt getting printed in output but i see thats not a good way to debug since the issue could be somewhere else
cap5lut
cap5lut21h ago
so first of all check now, if u can get it to work with either https://localhost:5001/<your-endpoint> or http://localhost:5000/<your-endpoint> if it now works, the problem was the iis express for sure
b
bOP21h ago
nope doesnt work when i use postman even the ones that did work before dont work
cap5lut
cap5lut21h ago
can u show me the url of an endpoint that worked previously?
b
bOP21h ago
says it could not send request error is socket hang up yeah holdup
cap5lut
cap5lut21h ago
(quick afk for ~4-5min)
b
bOP21h ago
yeah no worries but for exmaple i had a post request like this
http://localhost:53415/Api/User/new
http://localhost:53415/Api/User/new
with body that had "UserFour" in it ill be back too igtg rn but thanks for the help and thanks in advance for any further help
SleepWellPupper
SleepWellPupper21h ago
try using https and change the port to what it says in the logs so https://localhost:5001/Api/User/new
cap5lut
cap5lut21h ago
the weird thing is actually that u get a 404 after all, from various online resources (cant test it right now locally), it should be either a 400 response code (bad request), or ur model binding simply wouldnt work
b
bOP20h ago
i have to stick to http idk but yeah it says 404 not found
cap5lut
cap5lut20h ago
so does what SleepWellPupper mentioned work?
b
bOP20h ago
im supposed to stick to http not use https
cap5lut
cap5lut20h ago
the important point is that the port changed http://localhost:53415/Api/User/new was the old URI so the endpoint is /Api/User/new and the old base address was localhost:53415 for HTTP from the logs u showed earlier, u can see that the new base addresses are localhost:5001 for HTTPS and localhost:5000 for HTTP so the new URI for that endpoint should be: http://localhost:5000/Api/User/new
b
bOP20h ago
oh shiy yeah it works
cap5lut
cap5lut20h ago
test it like that first with a GET request that doesnt need a body, simply to test if it works
b
bOP20h ago
yeah it works let me try the one with json now wait it works now but for both iis express and without im so confused let me try with my console client now actually and see what happens
cap5lut
cap5lut20h ago
kk, ill take a quick ciggy break
b
bOP19h ago
uh shit idk whats happening im getting build errors. it worked the first time but its not working now and i tried cleaning. chats saying its cuz the server is still running in background or hung so imma look into that yeah it def is alrighty everythung works now thank u idk how or why it changed
cap5lut
cap5lut19h ago
did u close the process u started via dotnet run?
b
bOP19h ago
nope thats why i killed it now and iis express also works im so confused why its working now
cap5lut
cap5lut19h ago
i dont have enough information to tell ya why it works 😂
b
bOP19h ago
postman i think typo was why but then still idk how my client side code didnt work because it works now yeah fair enough and honestly as long as it works im happy lol
cap5lut
cap5lut19h ago
that 404 might be a hint that u were still running an old version, where that endpoint simply didnt exist yet. because it doesnt make sense to respond with that status code anyway, if ur problem is solved and u dont have any questions left, please mark this thread as that by $close ing it
MODiX
MODiX19h ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?