הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, December 7, 2015 9:30 PM
Greetings,
I have a HTTP request to POST JSON data to a web service.
How can I send this HTTP request with a visual basic code?
Regards
Michael
POST http://my.domain.com/api/v3/projects/8/work_packages HTTP/1.1
Content-Type: application/json
Accept-Encoding: gzip, deflate
Host: my.domain.com
Content-Length: 324
Expect: 100-continue
Cookie: _open_project_session=xxxxxxxxxxxxxxxxxxxxxxxxxxxx
{
"subject": "title",
"description": {
"format": "textile",
"raw": "example text"
},
"_links": {
"type": {
"href": "/api/v3/types/2"
},
"status": {
"href": "/api/v3/statuses/1"
},
"priority": {
"href": "/api/v3/priorities/1"
},
"assignee": {
"href": "/api/v3/users/1"
},
"responsible": {
"href": "/api/v3/users/1"
}
}
}
All replies (6)
Thursday, December 10, 2015 7:21 AM ✅Answered
Hi Michael-Markus,
Remove below code.
request.Expect = "100-continue"
and insert below code before send your request.
ServicePointManager.Expect100Continue = True
Best Regards,
Li Wang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Tuesday, December 8, 2015 9:42 AM
Hi Michael-Markus,
You could use HttpWebRequest to send http request. Code below is for your reference.
Dim request As HttpWebRequest = WebRequest.Create("http://my.domain.com/api/v3/projects/8/work_packages HTTP/1.1")
request.Method = "POST"
'add headers
request.Headers.Add("Content-Type", "application/json")
'add other headers
'....
'add body
Dim stream As Stream = request.GetRequestStream()
Dim jsonData As String = "your json"
Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(jsonData)
stream.Write(buffer, 0, buffer.Length)
'send request
request.GetResponse()
Best Regards,
Li Wang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Tuesday, December 8, 2015 8:42 PM
Hello Li Wang,
thank you for the information.
I have tried your code, but I get an error by this line:
request.Headers.Add("Content-Type", "application/json")
"The value is not a valid host header string"
What am I doing wrong?
Thank you!
Michael
Wednesday, December 9, 2015 8:55 AM
Hi Michael,
Change your code as follow.
Dim request As HttpWebRequest = WebRequest.Create("http://my.domain.com/api/v3/projects/8/work_packages HTTP/1.1")
request.Method = "POST"
'add headers
request.ContentType = "application/json;charset=utf-8"
request.Accept = "gzip,deflate"
request.Host = "my.domain.com"
request.ContentLength = 324
request.Expect = "100-continue"
request.Headers.Add("Cookie", "_open_project_session=xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
'add other headers
'....
'add body
Dim stream As Stream = request.GetRequestStream()
Dim jsonData As String = "your json"
Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(jsonData)
stream.Write(buffer, 0, buffer.Length)
'send request
request.GetResponse()
Best Regards,
Li Wang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Thursday, December 10, 2015 12:08 AM
Hello Li Wang,
I have run the code, but I get still an error at the line:
request.Headers.Add("Cookie", "_open_project_session=xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Argument Exception was unhandled by user code - 100-Continue can not set this parameter
Have you a idea, what are wrong?
Regards
Michael
Private Sub WPerzeugen_Click(sender As Object, e As EventArgs) Handles WPerzeugen.Click
Dim request As HttpWebRequest = WebRequest.Create("http://xyz.dyndns.org/api/v3/projects/8/work_packages HTTP/1.1")
request.Method = "POST"
request.ContentType = "application/json;charset=utf-8"
request.Accept = "gzip,deflate"
request.Host = "xyz.dyndns.org"
request.ContentLength = 324
request.Expect = "100-continue"
request.Headers.Add("Cookie", "_open_project_session=xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
'JSON Anhang zusammen stellen
Dim j As String = ""
j = j & "{ "
'Subject
j = """subject"": " & """" & "Hallo" & ""","
j = j & """_links"": { "
'WP Typ
j = j & """type"": { ""href"": ""/api/v3/types/""" & "2" & """ },"
'WP Status
j = j & """status"": { ""href"": ""/api/v3/statuses/""" & "1" & """ },"
'WP Priority
j = j & """priority"": { ""href"": ""/api/v3/priorities/""" & "1" & """ }"
'Abschluss
j = j & " } }"
Dim stream As Stream = request.GetRequestStream()
Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(j)
stream.Write(buffer, 0, buffer.Length)
'POST request absenden
request.GetResponse()
End Sub
Thursday, December 10, 2015 6:22 PM
...
Private Sub WPerzeugen_Click(sender As Object, e As EventArgs) Handles WPerzeugen.Click
Dim request As HttpWebRequest = WebRequest.Create("http://xyz.dyndns.org/api/v3/projects/8/work_packages HTTP/1.1")
request.Method = "POST"
...
I doubt that the protocol info was supposed to be included in the URL.
Setting the content type to application/json should be the only extra option needed; other than that it should be a standard http post.
You might consider using HttpClient instead of WebRequest/Response. There's an example here in C#, but it is short and simple so should be easy to reproduce in VB.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"