matt inckle

matt inckle

  • NA
  • 3
  • 1.7k

post data and manipulate results from JSON Webservice

Jan 25 2016 7:20 PM
 Hi Please can you help. I have been asked to see if I can integrate our systems with a 3rd party tool.
I've had the details from the 3rd party and it turns out I need to upload some data to a JSON Web service, then obviously manipulate the results

Example php has been supplied, but I always work in c# or VB and Ive made a start but starting to feel lost, can you help ?

''
public function runJsonRequestTest() {
$client = new \SoapClient(
"https://website.wsdl",
array(
'trace' => 1,
'exception' => 0
)
);
try {
$response = json_decode(
$client->json_request(
‘username’,
‘password’,
json_encode( //The request array
[
‘category’ => ‘api’,
‘type’ => ‘getMethods’,
‘data’ => [ ]
]
)
),
true
);
}
catch(\Exception $e) {
return "Soap request failed: {$e->getMessage()}

" . $client->__getLastResponse();
}
$string = print_r($response, true);
return $String;
'''
I've also got
'''
Example request array:
[
‘category’ => ‘api’,
‘type’ => ‘getMethods’,
]
Example return output:
Array (
[status] => COMPLETE
[data] => Array (
[0] => methodA
[1] => methodB
[2] => methodC
[3] => methodD) )
'''

so far I've got the below - but I get many errors and starting to get a bit lost - the below does not compile !

'''
public class ResultsTest
{
public string status { get; set; }
public List data { get; set; }
}

    public static async Task RunAsync(String Url, String Username, String Password, InstructionMessageModel instruction, String log)
{
_log = log;
_url = Url;
_username = Username;
_password = Password;
var baseAddress = new Uri(_url);
using (var client = new HttpClient())
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var auth = string.Format("{0}:{1}", _username, _password);
var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
try
{
HttpResponseMessage response = await client.PostAsJsonAsync(_url, instruction);

if (!response.IsSuccessStatusCode)
{
var errors = await response.Content.ReadAsAsync();
}
else
{
var jsonAsString = await response.Content.ReadAsAsync();
var output = JsonConvert.DeserializeObject(jsonAsString);
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
'''

Answers (2)