Question Issue with JSON POST using WebClient

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
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.
VB.NET:
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:
VB.NET:
{
    "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.
VB.NET:
Dim webClient As New System.Net.WebClient
webClient.Headers("User-Agent") = "MyAPIApp 1.0"
webClient.Headers("Content-Type") = "application/json"
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)
 
Last edited:

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
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:

VB.NET:
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:

VB.NET:
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.
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Thanks for your reply John! The API I'm working with doesn't specify the type of encoding to use. I tried setting the encoding to UTF8 as you suggested, but still the same issue (500 Internal Server Error).. Think I might have to contact them and see if they have any suggestions.
 
Top Bottom