zavikon
asked on
VB.Net WebRequest Code from JSON curl
I am running into a wall trying to generate vb.net code for an API web request for one of my vendors. Can someone help me translate the JSON curl into usable vb.net code? Thanks in advance.
curl -i -X POST -H "Authorization: ApiKey YOUR_API_KEY" \
-H "Content-Type: application/json" \
https://api-sandbox.com/v1 \
-d '{
"Candidate":{
"FirstName": "Test",
"LastName": "Candidate",
"Email": "candidate@example.com"
},
"Offer":{
"Products": ["RPT_BASIC"]
}
}'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think you might need a new question for this.
In any case to get you started;
whenever I have used the HttpWebRequest I have alwasy done;
In any case to get you started;
whenever I have used the HttpWebRequest I have alwasy done;
Dim request As HttpWebRequest = Nothing
Dim response As HttpWebResponse = Nothing
'set up your request here then call
response = request.GetResponse
'then use the response object
response.GetResponseStream() ' to get the stream.
This question is getting a little old. Do you have any updates or have you worked it out based on my solutions.
ASKER
I did not work it out based on your solutions... Following is my code that resolved the problem:
Protected Sub btnPostCreateReport_Click(sender As Object, e As System.EventArgs) Handles btnPostCreateReport.Click
Dim str_url As String
str_url = txtUrl.Text
Dim status As HttpStatusCode = HttpStatusCode.ExpectationFailed
Dim url As String = txtUrl.Text
Dim api_key As String = txtApiKey.Text
Dim jOwner As JObject = New JObject()
Dim jchild As JObject = New JObject()
Dim jchild2 As JObject = New JObject()
Dim jchild3 As JArray = New JArray()
jchild.Add("FirstName", txtFirstName.Text)
jchild.Add("LastName", txtLastName.Text)
jchild.Add("Email", txtEmail.Text)
jOwner.Add("Candidate", jchild)
jchild3.Add(txtProduct.Text)
jchild2.Add("Products", jchild3)
jOwner.Add("Offer", jchild2)
Dim content As String = jOwner.ToString(Formatting.None)
Dim response As Byte() = PostResponse(url, content, status)
Dim responseString As String
If response IsNot Nothing Then
responseString = System.Text.Encoding.UTF8.GetString(response)
Else
responseString = "NULL"
End If
txtCreateReportResponse.Text = responseString
Dim jsonDe = JsonConvert.DeserializeObject(responseString)
lblPostHttpResponse.Text = status
lblPostId.Text = jsonDe.item("Id")
lblPostCandidateFirstName.Text = jsonDe.item("Candidate")("FirstName")
lblPostCandidateLastName.Text = jsonDe.item("Candidate")("LastName")
lblPostCandidateEmail.Text = jsonDe.item("Candidate")("Email")
lblPostStatus.Text = jsonDe.item("Status")
lblPostStatusDetails.Text = jsonDe.item("StatusDetails")
lblPostSectionContainingAlerts.Text = jsonDe.item("SectionContainingAlerts")
lblPostReportViewerUrl.Text = jsonDe.item("ReportViewerUrl")
lblPostCadidateUrl.Text = jsonDe.item("CandidateUrl")
txtReportId.Text = lblPostId.Text
End Sub
Private Sub btnPostRetrieveReport_Click(sender As Object, e As EventArgs) Handles btnPostRetrieveReport.Click
Dim str_url As String
str_url = txtUrl.Text
Dim status As HttpStatusCode = HttpStatusCode.ExpectationFailed
Dim url As String = txtUrl.Text
Dim api_key As String = txtApiKey.Text
Dim content As String = txtReportId.Text
Dim response As String = GetResponse(url, content, status)
Dim responseString As String
If response IsNot Nothing Then
responseString = response
Else
responseString = "NULL"
End If
txtRetrieveReportResponse.Text = responseString
Dim jsonDe = JsonConvert.DeserializeObject(responseString)
lblGetHttpResponse.Text = status
lblGetId.Text = jsonDe.item("Id")
lblGetCandidateFirstName.Text = jsonDe.item("Candidate")("FirstName")
lblGetCandidateLastName.Text = jsonDe.item("Candidate")("LastName")
lblGetCandidateEmail.Text = jsonDe.item("Candidate")("Email")
lblGetStatus.Text = jsonDe.item("Status")
lblGetStatusDetails.Text = jsonDe.item("StatusDetails")
lblGetSectionContainingAlerts.Text = jsonDe.item("SectionContainingAlerts")
lblGetReportViewerUrl.Text = jsonDe.item("ReportViewerUrl")
lblGetCandidateUrl.Text = jsonDe.item("CandidateUrl")
End Sub
Public Function PostResponse(url As String, content As String, ByRef statusCode As HttpStatusCode) As Byte()
Dim responseFromServer As Byte() = Nothing
Dim dataStream As Stream = Nothing
Try
Dim request As WebRequest = WebRequest.Create(url)
request.Timeout = 120000
request.Method = "POST"
request.Headers("Authorization") = "ApiKey " + txtApiKey.Text
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(content)
request.ContentType = "application/json"
request.ContentLength = byteArray.Length
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim ms As New MemoryStream()
Dim thisRead As Integer = 0
Dim buff As Byte() = New Byte(1023) {}
Do
thisRead = dataStream.Read(buff, 0, buff.Length)
If thisRead = 0 Then
Exit Do
End If
ms.Write(buff, 0, thisRead)
Loop While True
responseFromServer = ms.ToArray()
dataStream.Close()
response.Close()
statusCode = HttpStatusCode.OK
Catch ex As WebException
If ex.Response IsNot Nothing Then
dataStream = ex.Response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim resp As String = reader.ReadToEnd()
statusCode = DirectCast(ex.Response, HttpWebResponse).StatusCode
Else
Dim resp As String = ""
statusCode = HttpStatusCode.ExpectationFailed
End If
Catch ex As Exception
statusCode = HttpStatusCode.ExpectationFailed
End Try
Return responseFromServer
End Function
Public Function GetResponse(url As String, content As String, ByRef statusCode As HttpStatusCode) As String
Dim responseFromServer As String = ""
Dim dataStream As Stream = Nothing
Try
url = url + content
Dim request As WebRequest = WebRequest.Create(url)
request.Timeout = 120000
request.Method = "GET"
request.Headers("Authorization") = "ApiKey " + txtApiKey.Text
Dim response As WebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
' Read the content.
responseFromServer = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams and the response.
reader.Close()
response.Close()
statusCode = HttpStatusCode.OK
Catch ex As WebException
If ex.Response IsNot Nothing Then
dataStream = ex.Response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim resp As String = reader.ReadToEnd()
statusCode = DirectCast(ex.Response, HttpWebResponse).StatusCode
Else
Dim resp As String = ""
statusCode = HttpStatusCode.ExpectationFailed
End If
Catch ex As Exception
statusCode = HttpStatusCode.ExpectationFailed
End Try
Return responseFromServer
End Function
Zavikon - your original question is about the serialization / de-serialization of JSON. I answered that in my first comment (Link) and suggested you use a 3rd party library such as Json.net. In your solution you use JOBJECT which as you can see from this link is an object from the library I suggested you use.
Why do you think that you solved your original question you had?
The other comments I made which you may or may not have used related to the connection or communication with some service which as I also let you know probably would need a new question as it is a fairly different topic.
Why do you think that you solved your original question you had?
The other comments I made which you may or may not have used related to the connection or communication with some service which as I also let you know probably would need a new question as it is a fairly different topic.
ASKER
Open in new window