I've probably made a really silly mistake, I am able to make a successful POST request to server. I am also able to get a response from server 201 as well as being able to view the json("requestid")
. I am able to deserialize the JSON and parse requestid
as a string (public string requestID
). I have a timer (timer1
) set up to poll the server every 1 second, the polling should start successfully if 201 created and does. However the problem I am having is that it does not include the requestid
. Would someone be able to advise and tell me where I had gone wrong please?
namespace RestAPI
{
public enum httpVerb
{
GET,
POST,
PUT,
DELETE
}
class RESTAPI
{
public string endPoint { get; set; }
public httpVerb httpMethod { get; set; }
public string userPassword { get; set; }
public int sendAmount { get; set; }
public string location { get; set; }
public string requestId { get; set; }
public RESTAPI()
{
endPoint = string.Empty;
httpMethod = httpVerb.GET;
userPassword = string.Empty;
//requestId = string.Empty;
}
public string makeRequest()
{
string strResponseValue = string.Empty;
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = httpMethod.ToString();
request.ContentType = "application/json";
request.Accept = "application/connect.v1+json";
String username = "mokhan";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + userPassword));
request.Headers.Add("Authorization", "Basic " + encoded);
if(httpMethod == httpVerb.POST)
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"transactionType\":\"SALE\"," + "\"amount\":" + sendAmount + "," +
"\"currency\":\"GBP\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse responseback = (HttpWebResponse)request.GetResponse();
//string result;
using (StreamReader rdr = new StreamReader(responseback.GetResponseStream()))
{
result = rdr.ReadToEnd();
}
if (responseback.StatusCode == HttpStatusCode.Created)
{
dynamic jsonObj = JsonConvert.DeserializeObject(result);
requestId = jsonObj.requestId.ToString();
return requestId;
}
return result;
}
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
strResponseValue = reader.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
}
finally
{
if (response != null)
{
((IDisposable)response).Dispose();
}
}
return strResponseValue;
}
}
}
This code below shows my POST and GET requests to server, I have added the timer method to start in my POST request after I get a response and have added the code for polling in my timer method. I have also set string transactionid = rclient.requestId;
and the called on rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text + "/transactions/" + transactionid;
to poll the server every 1 second but for some reason it's not picking up transactionid
.
namespace RestAPI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void go_Click(object sender, EventArgs e)
{
RESTAPI rclient = new RESTAPI();
rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text;
rclient.userPassword = txtbox2.Text;
debugOutput("REQUEST SENT");
string strResponse = string.Empty;
strResponse = rclient.makeRequest();
debugOutput(strResponse);
}
private void debugOutput(string strDebugText)
{
try
{
System.Diagnostics.Debug.Write(strDebugText + Environment.NewLine);
txtBoxResponse.Text = txtBoxResponse.Text + strDebugText + Environment.NewLine;
txtBoxResponse.SelectionStart = txtBoxResponse.TextLength;
txtBoxResponse.ScrollToCaret();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message, ToString() + Environment.NewLine);
}
}
private void txtBox_TextChanged(object sender, EventArgs e)
{
}
private void Test_Click(object sender, EventArgs e)
{
RESTAPI rclient = new RESTAPI();
rclient.httpMethod = httpVerb.POST;
rclient.sendAmount = Convert.ToInt32(amount.Text);
rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text + "/transactions";
rclient.userPassword = txtbox2.Text;
debugOutput("REQUEST SENT");
string strResponse = string.Empty;
strResponse = rclient.makeRequest();
debugOutput(strResponse);
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
RESTAPI rclient = new RESTAPI();
rclient.httpMethod = httpVerb.GET;
string transactionid = rclient.requestId;
rclient.endPoint = "https://" + txtBox.Text + "/pac" + "/terminals/" + txtBox3.Text + "/transactions/" + transactionid;
debugOutput("REQUEST SENT");
string strResponse = string.Empty;
strResponse = rclient.makeRequest();
debugOutput(strResponse);
}
}
}
Just solved the issue, in my public class RESTAPI, i un-commented this //requestId = string.Empty; once done, i was able to pick up the request ID as a string and then call it in my timer string transactionid = rclient.requestId;