0

I am trying to read image data from a third party RESTful API. The problem that I am running is the data is being returned in a data structure with the content type of "application/json".

Is it possible to retrieve the image and convert it to a jpeg after parsing out the rest of the json to get the image string itself?

The string begins with the following characters -> ����\u0000\u0010JFIF

I've tried the following, but the image comes out unviewable.

var data = JsonConvert.DeserializeObject<ImageReturn>(response.Content);
var img = data.record[0].tables.photo.imagedata;
var bytes = Encoding.UTF32.GetBytes(img);
var file = System.IO.File.Create(@"C:\Users\example\Desktop\test.jpeg");

Where imagedata is being deserialized as a string initially.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user1856254
  • 1
  • 1
  • 1
  • 3
    What does the APIs docu tell you about the returned type? It does not look like json. – Ralf Aug 04 '23 at 15:23
  • 1
    Even if we assume that each byte occupying the space under "imagedata", a JSON parser would no doubt eventually hit something that it interprets as backslash and break, or quote and escape the "string". Some encoding (e.g. base64 or hex) will be required on the API server side in order to serve images to you in a form that you can consume them successfully. – ProgrammingLlama Aug 04 '23 at 15:30
  • Unfortunately this is mostly undocumented use of the API and we don't have access server side to be able to change the way the API works. If I'm going to be able to read these images, they will need to be converted from the data we are currently retrieving, or we'll have to find some other way of pulling these images. – user1856254 Aug 04 '23 at 16:02
  • Your first 4 characters are showing up as untranslated and your next two are not bytes - can you show the hex byte values for the beginning of the string? A JFIF file should start with something like `0xFF 0xD8 0xFF 0xE0 0x00 0x10 J F I F 0x00`, so you might have close to what you need. – NetMage Aug 04 '23 at 20:02
  • please post the response of the api, payload example and header values of the response – Zinov Aug 05 '23 at 11:42

0 Answers0