0

I'm trying to use the Bulk Import API endpoint within Marketo to fetch the warnings and failures associated with a bulk job I created within Marketo.

The output of these jobs is stated as an ObservableOfInputStreamContent, yet the response of these endpoints returns a csv string (with header column), that's not a JSON object. To compound to this problem, we are using the generated swagger files with the swagger definition file provided by Marketo. These generated c# client side files have the ObservableOfInputStreamContent object, but it's an empty object. I'm not sure if this is intended, or a mistake on Marketo's side. The generated files will attempt to use Newtonsoft.Json.JsonConvert.DeserializeObject<ObservableOfInputStreamContent>(responseText, JsonSerializerSettings); to deserialize the API response to the ObservableOfInputStreamContent.

Generated code that deserializes the API response:

var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
try
{
    var typedBody = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseText, JsonSerializerSettings);
    return new ObjectResponseResult<T>(typedBody, responseText);
}
catch (Newtonsoft.Json.JsonException exception)
{
    var message = "Could not deserialize the response body string as " + typeof(T).FullName + ".";
    throw new ApiException(message, (int)response.StatusCode, responseText, headers, exception);
}

Problem one is that the API does not return JSON to begin with (eg):

address,city,country,
123 lane, new york, USA
745 street, new york, USA

This call will return this error because of this:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: a. Path '', line 0, position 0.

The second issue is that ObservableOfInputStreamContent is defined as an empty object to begin with in the generated files. So if the API response was valid JSON, I don't think it would know how to convert to that empty ObservableOfInputStreamContent object. The good news about the generated code is that it gives me an option to extend the ObservableOfInputStreamContent because it's defined as a partial class.

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.11.0 (Newtonsoft.Json v11.0.0.0)")]
public partial class ObservableOfInputStreamContent
{

}

That said, is there any possible way I can use the JsonSerializerSettings to get around this issue? Could I extended the ObservableOfInputStreamContent class to hold a string property and then create my own JsonConverter to convert the string returned from the API into the new ObservableOfInputStreamContent?

Jimenemex
  • 3,104
  • 3
  • 24
  • 56
  • You cannot parse json until all the data is read. json has open/close tags and the json serializer will fail if you do not have matching open and close tags. – jdweng Oct 23 '20 at 21:04
  • @jdweng It fails because the response from the API is not JSON at all. There is no open and close tags `{}`. – Jimenemex Oct 23 '20 at 21:07
  • The response depends on your URL. What is your URL? See : https://developers.marketo.com/rest-api/base-url/ – jdweng Oct 24 '20 at 03:56
  • @jdweng The `BaseUrl` does not alter the response in any way other then pulling in the correct Marketo instance's data. :) – Jimenemex Oct 26 '20 at 00:16
  • The URL doesn't contain any parameter (after a question mark)? Do you have to add any http headers to the post? – jdweng Oct 26 '20 at 00:20
  • Sorry if I put it confusingly - The request works, it's the response object from those warnings and failures that is different then what is documented by Marketo. The response returns a csv string, while the documentation says it's supposed to be an empty `ObservableOfInputStreamContent` object. I am asking how I can get around this, if I can, using the `JsonSerializerSettings`. – Jimenemex Oct 26 '20 at 02:30
  • The format of the response is determined by the request. So you need to fix the request. – jdweng Oct 26 '20 at 08:09
  • What does the request look like? – Patrick Mcvay Nov 02 '20 at 14:41
  • share the code where you instantiate your request – Benzara Tahar Nov 02 '20 at 16:00
  • why do you have to pass by `JsonConvert`, why not writing a custom deserializer that parse the csv text reponse like it is done here https://stackoverflow.com/questions/26790477/read-csv-to-list-of-objects? – Benzara Tahar Nov 02 '20 at 16:25

0 Answers0