0

I need to pass files as the body of a request to an available API. In the process of sending the request, I have a function that serializes parameters to JSON.

The point is that in c# we cannot serialize a file into JSON. So my question is that which type of data should I convert to? And is there any sample for it

I got the suggestion that converting into the stream but I don't figure it out.

The type of files are CSV, xlsx, and xml

nl-x
  • 11,762
  • 7
  • 33
  • 61
Đức Tú
  • 23
  • 1
  • 5
  • 1
    Hi, it is need to known how API take this file. It should be some documentation for this API. You may try convert file into Base64. – Roma Ruzich Sep 27 '21 at 10:17
  • 1
    `in c# we cannot serialize a file into JSON.` of course we can. Just like any other language. The question itself is rather ... weird though. JSON is text. And unless that file contains JSON, you need to modify it somehow. If it's binary, you need to encode it. None of this matters though. *What does the API expect?* – Panagiotis Kanavos Sep 27 '21 at 10:18
  • @Roma Ruzich Can you show me more details – Đức Tú Sep 27 '21 at 10:20
  • @Panagiotis actually I need to pass xlsx file, csv file but not text file. Is there any solution for those particular types – Đức Tú Sep 27 '21 at 10:21
  • @ĐứcTú **you** have to show us more details. The question makes little sense as is. What does the API expect? JSON? Bytes? If JSON, in what form? And what do you mean by "pass files as the body"? JSON doesn't deal with files. Does the API expects you to send bytes in a certain way? Or perhaps this has nothing at all to do with JSON and you want to upload some files? – Panagiotis Kanavos Sep 27 '21 at 10:21
  • @ĐứcTú `actually I need to pass xlsx file,` pass to *what*? You can make a plain old `PostAsync` and send the file contents as binary data. You can create a `FORM POST` to upload files. JSON is text though. You *must* convert the binary files to something else first – Panagiotis Kanavos Sep 27 '21 at 10:22
  • look here https://stackoverflow.com/questions/25919387/converting-file-into-base64string-and-back-again – Roma Ruzich Sep 27 '21 at 10:22
  • This totally depends on the API implementation, so you will need to consult the API documentation. It could be that the file should be sent as the POST data. It will not be JSON though. See for that example https://stackoverflow.com/questions/1131425/send-a-file-via-http-post-with-c-sharp Why do you think it should be JSON? – nl-x Sep 27 '21 at 10:26
  • @Panagiotis Yes, I want to upload a file. So generally, could you suggest to me the solution for upload a file to the server. Sorry for the "pass files as the body", my bad, I mean that I want to send the file inside the body of the request. – Đức Tú Sep 27 '21 at 10:26
  • @ĐứcTú as everyone else said, we can't even guess what that completely custom API expects. We can't tell you how to send files to it. Almost *nobody* accepts files inside JSON because BASE64 encoding used for this increases the file size by *at least* 33%. If you want to upload files the same way you do with a FORM POST, you can use `HttpClient.PostAsync` with a `MultipartFormDataContent` as [shown here](https://stackoverflow.com/a/53190314/134204) – Panagiotis Kanavos Sep 27 '21 at 12:46
  • @ĐứcTú if you really, really want to post a JSON object, you can load the file contents into a `byte[]` property. The JSON serializer will convert it to BASE64. – Panagiotis Kanavos Sep 27 '21 at 12:47

0 Answers0