0

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!

Scott
  • 3,663
  • 8
  • 33
  • 56
  • 1
    You need to change your `ContentType` header to `application/json`. You are telling the API that you are sending `application/x-www-form-urlencoded` but that's obviously not the same thing as JSON. Note that the `Accept` header tells the server what kind of response you will accept in return, not what you are sending. – Brian Rogers Sep 01 '20 at 22:32
  • That's a good catch - and I'm now getting a 404 error. I suspect that may be an endpoint problem – Scott Sep 01 '20 at 22:56
  • In your URL you have `@Token` -- is that supposed to be replaced with something? – Brian Rogers Sep 02 '20 at 00:12
  • Note that `NameValueCollection` requires a converter to serialize correctly. See [how to convert NameValueCollection to JSON string?](https://stackoverflow.com/q/7003740/3744182) and [ArgumentNullException in Json.NET 6.0.7 when deserializing into NameValueCollection](https://stackoverflow.com/q/27828350/3744182). I don't actually see where you are using a `NameValueCollection` in your code though. – dbc Sep 02 '20 at 16:05
  • Brian and dbc thanks for the questions. Yes @Token is replaced with the user token. I left it that code for simplicity since I know it works. Also I have a function that converts the nameValueCollection to string and I choose not to show that code since it wasn't the point of the question, the output string was. However if the output string used is incorrect I can revisit that. Sorry for the confusion – Scott Sep 03 '20 at 11:37

0 Answers0