2

I have this code:

String json = "{\"name\":\"listFiles\"}";

// Wrap our JSON inside a StringContent which then can be used by the HttpClient class
HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

HttpResponseMessage response = client.PostAsync(endPoint, httpContent).Result;
response.EnsureSuccessStatusCode();

when this post is done, I can see that server received a call but the content is blank (content size is correct, but it has no data).

I tried to investigate if the httpContent has the json data, but I can not see any data.

Why this code, post blank data to server and why I can not see any data inside httpContent?

How can I extract json back from httpContent, so I can check that it is properly initialized?

Value httpContent

where is its content?

Edit1

I checked the server and I am getting this data:

POST /osc/command/execute HTTP/1.1
Content-Type: application/json
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/106.2.1.0
Host: 192.168.1.138
Content-Length: 32
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

when I am sending data using restshart library as follow:

var client = new RestClient("http://192.168.1.138/osc/command/execute");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\n  \"name\":\"camera.listFiles\"\n}\n", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

when I post the same code using postman, I am getting this on server:

POST /osc/command/execute HTTP/1.1
cache-control: no-cache
Postman-Token: 83ecd3ec-a85d-41a6-ab0e-029ee1690cce
Content-Type: application/json
User-Agent: PostmanRuntime/6.3.2
Accept: */*
Host: 192.168.1.138
accept-encoding: gzip, deflate
content-length: 32
Connection: keep-alive

{
 "name":"camera.listFiles"
}

so why there is nothing at the end of packet that I am reading with content that I posted?

Edit 2

I posted the data to "http://httpbin.org/post" and the result that I am getting is:

{
 "args": {},
 "data": "{\"name\":\"listFiles\"}",
 "files": {},
 "form": {},
 "headers": {
   "Accept": "application/json",
   "Connection": "close",
   "Content-Length": "20",
   "Content-Type": "application/json; charset=utf-8",
   "Expect": "100-continue",
   "Host": "httpbin.org"
 },
 "json": {
   "name": "listFiles"
 },
 "origin": "91.240.174.154",
 "url": "http://httpbin.org/post"
}

I can not see any payload here, but I can see the json with my data in it. The question is that why there is no payload as explained in this document: How are parameters sent in an HTTP POST request?

note1

What I can see is that when I posted to "http://httpbin.org/post", the connection is closed, but when I posted to my server, the connection is keep-alive, what is the difference? Is there any possibility that I am not reading all data from client on server and I should wait for another packet?

mans
  • 17,104
  • 45
  • 172
  • 321
  • how do you read data on the server side? – Left panda Jan 30 '18 at 14:55
  • How are you checking the content is blank on the server? This code looks fine (other than you are calling `.Result` instead of doing proper async calls) – DavidG Jan 30 '18 at 14:56
  • The server is my code, so I put a debug code and read data. When I post data using a python code, I am getting them at the server, but when I am posting using C#, I am not getting the json,only I get the length. – mans Jan 30 '18 at 14:57
  • If you cannot show us the server code, we will be forced to close the question though - there isn't enough information here to guess at an answer. – DavidG Jan 30 '18 at 14:58
  • @DavidG I don't want an async method, so I used result. Is there any way that I make it sync call without using .result? – mans Jan 30 '18 at 14:59
  • @DavidG: how can I see the content of httpContent? How can I see what is posted to server? – mans Jan 30 '18 at 15:00
  • @mans how can we know ? you didn't post anything or any information about the server side . – Left panda Jan 30 '18 at 15:01
  • wait a minute, what he post is the server side code right? that doesnt seems javascript to me – LPZadkiel Jan 30 '18 at 15:03
  • @Leftpanda The server code is a full HTTP server written in boost.asio and it respond to request from Python properly. If I can see what is inside httpcontent, that is the first stage of debugging. – mans Jan 30 '18 at 15:04
  • @LPZadkiel This is the client side that should post a json to server. – mans Jan 30 '18 at 15:04
  • OK, I'll say it again: *there is nothing wrong with the code you have shown above*. This means that there is either something wrong with your server or a mismatch in what you are sending vs what is expected. Either way we need to see details of the server to help you. – DavidG Jan 30 '18 at 15:06
  • @DavidG Can you please help me to rad back json that I put in stringContent from it? I looked at the chttpContent in debugger and I can not see any data there, all fields are bank or have unrelated data. – mans Jan 30 '18 at 15:09
  • did you not know how to debug? usually every IDE has debug for javascript and even the browsers have debug tools for javascript, try to debug the javascript to check if you are really sending the data – LPZadkiel Jan 30 '18 at 15:10
  • No! There's nothing wrong with that code, I'm not going to try to help you debug code that is not broken. – DavidG Jan 30 '18 at 15:10
  • yeah the code looks fine but if the server is not sendind an error that means that there is no mismatch on parameters, could be another thing but appearently he hasnt debug anything so... debug please – LPZadkiel Jan 30 '18 at 15:13
  • @LPZadkiel Which javascrip are you speaking? there is no JS, it is C# – mans Jan 30 '18 at 15:14
  • Is there any service that I can post my data to it and it show me what data it receives? This way I can test that the problem is my client code or server code? – mans Jan 30 '18 at 15:18
  • You should not be calling .Result like that. If you do not want to call an async method using await, you'll need to use a SynchronizationContext to wrap the call to an async method. An example of how to implement this is shown here https://gist.github.com/ChrisMcKee/6664438 – Emma Middlebrook Jan 30 '18 at 15:21
  • @mans Your content is properly sent to the server: see my screenshot: http://prntscr.com/i7pogl – Roman Koliada Jan 30 '18 at 15:27
  • @mans You can inspect you content like this: `string content = await httpContent.ReadAsStringAsync();` – Roman Koliada Jan 30 '18 at 15:41
  • @mans i will say this again, did you check if you are really sending the data to the server? maybe you are sending an empty parameter so thats why you are not seeing anything – LPZadkiel Jan 30 '18 at 17:29
  • @LPZadkiel Please see updated question. I also used new way of posting data with restsharp library with the same result. – mans Jan 30 '18 at 17:37
  • the thing is you use "post data", that usually means from client to server, that's why i ask before if this was sending from client to server or the other way around sorry about the other comments i was so confused – LPZadkiel Jan 30 '18 at 17:40
  • @LPZadkiel It is sent from client to server. The client code is the one that you can see, but on server I am not getting the data when I sent via C#, if I sent via postman, I am getting the data on server. – mans Jan 31 '18 at 09:39
  • as far as i know, you dont have to set the response on httpcontext, c# add returned value to the context internally, so you just have to return a string and if you want to return a object as json you can use JavaScriptSerializer for that – LPZadkiel Jan 31 '18 at 11:39

1 Answers1

1

I have no reputation to comment on your post (which is where i wanted to place this comment - so please go easy on me regarding down votes). But we had an issue almost exactly the same as yours today. After conferring with a co-worker on this, we were able to get the JSON response in essence by changing your line:

HttpResponseMessage response = client.PostAsync(endPoint, httpContent).Result;

to

var response = client.PostAsync(endPoint, httpContent).Result.Content.ReadAsStringAsync();

That revealed our JSON

xTkAx
  • 11
  • 1