0

The webservice is returning a list of JSON objects. The JSON object is like this:

"id": "1",
"stanze": 2,
"dotazione": [{
    "id": 31,
    "nome": "Wi-Fi"
},
{
    "id": 23,
    "nome": "Garden"
},
{
    "id": 3,
    "nome": "TV"
}]

The RootObject is a class, generated online automatically for the JSON object structure.

List<RootObject> allObj = new List<RootObject>(); 

Finally, I want to deserialize the JSON response into a List of RootObject(s)

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(allObj.ToString());

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.thewebservice.com/api");

I've managed to add the header parameters for the web service request.

request.Method = "POST";
request.MediaType = "application/json";
request.ContentType = "application/vnd.api+json";

1st issue: Don't think the ContentLength is set correctly

request.ContentLength = data.Length;/// ????
request.Accept = "application/vnd.api+json";

These are the parameters for header

request.Headers.Add("X-USER", "myuser");
request.Headers.Add("X-PASS", "myuser2018Net");
request.Headers.Add("X-WHAT", "ALLOGGILIST");
request.Headers.Add("X-ACTION", "GET");

2nd issue How to set a parameter for body? e.g. parameter name = lingua, value = EN

I tested the webservice using SOAP. And I put the above parameter in the section below the Media Type. enter image description here

Florin M.
  • 2,159
  • 4
  • 39
  • 97

2 Answers2

0

As per the MSDN site make sure the value of data.Length is not -1. https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength(v=vs.110).aspx

You can use below for setting the language:

WebHeaderCollection myWebHeaderCollection = request.Headers;    

//Include English in the Accept-Langauge header. 
myWebHeaderCollection.Add("Accept-Language","en;q=0.8");
Alin
  • 314
  • 1
  • 3
  • 9
  • And what if I want to introduce a body parameter called 'Operation' with the value 'RandomText'? I – Florin M. Feb 08 '18 at 08:43
  • @FlorinM. You can add custom header `myWebHeaderCollection.Add("Operation","RandomText");` Why do you want it to send in body ? If you want to send it in body then you need to covert the json into request model and use it. https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api – Alin Feb 08 '18 at 08:59
  • I tried what you said and I did get 405 error. Also, in the SOAP, if I put the parameter 'lingua' in the header I get no response. – Florin M. Feb 08 '18 at 09:03
0

If you are using WCF as the web service, you may need to declare the method using WebInvoke like this :

[WebInvoke(
UriTemplate = "yourpath",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]

Focus on the "BodyStyle", because that param which will define how you pass your parameter by using the BODY.

This post explains it :

Can't Pass JSON Post Data to WCF REST Service using Fiddler

Hope it helps

  • I'm not using WCF – Florin M. Feb 08 '18 at 10:56
  • You need to post the request body the way the web service allows you to. You have 2 options now, modify the web service or change the way you send the request body. It's too bad that I don't have the information about the technology you are using to develop the web service. Or maybe you can't touch it either? In this case, just learn from the web service developer about how to post properly. Sorry cannot help you more. – Erlangga Hasto Handoko Feb 09 '18 at 01:59