I'm working in VB.NET and need to hit an API endpoint with an array of JSON data, but I'm getting an error returned. I've been hitting a very similar endpoint with a single NameValueCollection key and it works fine - I'm wondering if I'm just encoding the Json incorrectly and maybe an API that's more forgiving of incorrectly formed data for a single datapoint is less so for an array of data. I've reached out to the API owners as well in their help forum but getting a response there is hit or miss.
Here is a simplified look at the code I'm successfully using to add a single item to inventory. It requires only one piece of information in the formData called "quantity". The ID number of the set being entered goes through the url:
Dim sURL="https://rebrickable.com/api/v3/users/<UserToken>/sets/9132-1?key=<UserKey>"
Dim httpObject = DirectCast(WebRequest.Create(sURL), HttpWebRequest)
httpObject.Method = "PUT"
httpObject.ContentType = "application/x-www-form-urlencoded"
httpObject.Accept = "application/json"
Dim byte1 As Byte() = Encoding.UTF8.GetBytes("quantity=1")
httpObject.ContentLength = byte1.Length
httpObject.GetRequestStream().Write(byte1, 0, byte1.Count)
Dim resp As Stream = httpObject.GetResponse.GetResponseStream
I'm trying to access the sync endpoint, which accepts a Json array of sets and quantities and replaces the online inventory with the supplied dataset. See the docs here for reference (https://rebrickable.com/api/v3/docs/)
The docs explicitly have an example Json which I've decided to try to use to test my API call and so far it's failing. In the code below I've saved it as dataStg:
Dim sURL="https://rebrickable.com/api/v3/users/@Token/sets/sync/?key=<UserKey>"
Dim httpObject = DirectCast(WebRequest.Create(sURL), HttpWebRequest)
httpObject.Method = "POST"
httpObject.ContentType = "application/x-www-form-urlencoded"
httpObject.Accept = "application/json"
Dim dataStg = "[{""set_num"":""8043-1"", ""quantity"": 1}, {""set_num"":""8110-1"", ""quantity"": 2, ""include_spares"": ""False""}]"
Dim byte1 As Byte() = Encoding.UTF8.GetBytes(dataStg)
httpObject.ContentLength = byte1.Length
httpObject.GetRequestStream().Write(byte1, 0, byte1.Count)
Dim resp As Stream = httpObject.GetResponse.GetResponseStream
Obviously the problem may be specific to the API, but I'm doing a lot here with the HttpWebRequest, UTF8 encoding, etc and I was hoping there was something I was doing wrong from a .NET or http perspective. Thanks!