-
Sep 17th, 2020, 09:53 PM
#1
Thread Starter
Addicted Member
Issue with JSON POST using WebClient
I'm messing around with an API that uses JSON, but can't seem to make it POST properly. This particular API authenticates my account using three pieces of information which must be passed in the header. The service offers a basic endpoint you can GET to make sure your information is being passed properly, and when I try that it works fine. However, when I try to POST something it keeps giving me Internal Server errors..
Here is the working GET request which confirms to me that my account info is correct and the headers are being passed properly.
Code:
Dim webClient As New System.Net.WebClient
webClient.Headers("User-Agent") = "MyAPIApp 1.0"
webClient.Headers("X-Auth-Reseller") = txtxAuthReseller.Text
webClient.Headers("X-Auth-User") = txtxAuthUser.Text
webClient.Headers("X-Auth-Password") = txtxAuthPassword.Text
Dim result As String = webClient.DownloadString("https://" & My.Settings.Hostname & "/authenticated")
MsgBox(result)
Next, I want to make my first POST request. I need to pass the following JSON:
Code:
{
"group_name" : "Default Name Servers",
"is_default" : true,
"name_servers" : [
"ns1.example.com",
"ns2.example.com"
]
}
Here is the POST code I am trying. This gives me: The remote server returned an error: (500) Internal Server Error.
Code:
Dim webClient As New System.Net.WebClient
webClient.Headers("User-Agent") = "MyAPIApp 1.0"
webClient.Headers("X-Auth-Reseller") = txtxAuthReseller.Text
webClient.Headers("X-Auth-User") = txtxAuthUser.Text
webClient.Headers("X-Auth-Password") = txtxAuthPassword.Text
Dim result As String = webClient.UploadString("https://" & My.Settings.Hostname & "/dom/name-server-group", "{""group_name"" : ""Default Name Servers"", ""is_default"" : true, ""name_servers"" : [""ns1.sedoparking.com"", ""ns2.sedoparking.com""]}")
MsgBox(result)
-
Sep 17th, 2020, 09:59 PM
#2
Re: Issue with JSON POST using WebClient
Try adding this:
vb.net Code:
webClient.Headers("Content-Type") = "application/json"
-
Sep 17th, 2020, 10:14 PM
#3
Thread Starter
Addicted Member
Re: Issue with JSON POST using WebClient
Thanks for the reply jmcilhinney. It still gives the internal server error.
Last edited by digitaldrew; Sep 17th, 2020 at 10:17 PM.
-
Sep 18th, 2020, 01:06 AM
#4
Thread Starter
Addicted Member
Re: Issue with JSON POST using WebClient
I don't know if this will be much help, but they put a small example on this page which uses the same endpoint as my initial GET (/authenticated) and they include a few other things in their header (although they never specify it's required)..Here is what theirs looks like:
Code:
X-Auth-Reseller: reseller
X-Auth-User: user
X-Auth-Password: password
Host: api.yay.com
Connection: close
User-Agent: MyAPIApp 1.0
So I decided to add the others into my code:
Code:
Using webClient As New System.Net.WebClient
webClient.Headers("User-Agent") = "MyAPIApp 1.0"
webClient.Headers("Content-Type") = "application/json"
webClient.Headers("Host") = hostName
webClient.Headers("Connection") = "close"
webClient.Headers("X-Auth-Reseller") = txtxAuthReseller.Text
webClient.Headers("X-Auth-User") = txtxAuthUser.Text
webClient.Headers("X-Auth-Password") = txtxAuthPassword.Text
Dim result As String = webClient.UploadString("https://" & hostName & "/dom/name-server-group", "{""group_name"" : ""Default Name Servers"", ""is_default"" : true, ""name_servers"" : [""" & txtNS1.Text & """, """ & txtNS2.Text & """]}")
MsgBox(result)
End Using
When adding the Connection header I now get the follow error instead of a 500 Internal Server Error: An exception occurred during a WebClient request. Once I remove the connection part, it's back to 500 Internal Server Error..
Feeling a bit lost.. I've done a POST with JSON in the past and didn't have nearly the trouble I am this time around. I'm not that great with headers, but once I could do the GET I thought for sure the POST wouldn't be too difficult.
-
Sep 18th, 2020, 07:34 AM
#5
Re: Issue with JSON POST using WebClient
It's possible it's solely a problem on their end....you're getting a 500 internal Server Error... not a bad request... so your request is being accepted, but then something fails on their end.
Also, I'd assemble your json into a variable and print it out before sending.... just in case... one misplaced " and that's going to wreck....
Hmmmm.... I looked at that link...AND PT2 they linked to ... they used a PUT to update , not post... so.... hmmm.....
Are you trying to interact with YAY.com or just used that as an exaample? IF using as an example, and you're actually interacting with a different host, then you need to find the API guide for that host.
-tg
-
Sep 18th, 2020, 01:21 PM
#6
Thread Starter
Addicted Member
Re: Issue with JSON POST using WebClient
Hey techgnome, thanks for your reply! I did look at my JSON before sending (a few times) just to make sure everything matched up and it did, but I will double check that again. I'm trying to interact with Yay.com. The specific endpoint I'm using in my request is located on this page. You will see they have different end points depending on the call you want to make - some with GET, some with POST and some with PUT. The call I am trying to work with is from the only POST endpoint on that page (/dom/name-server-group)...Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|