user2806116
user2806116

Reputation: 65

how to return a file that contains the output of the api in json format?

i have an api that gives the output in json format as default. i want the output to be saved in a file and return this file.

now my output is of the form:

[
  {"FName":"Folder1"},
  {"FName":"Folder2"},
  {"FName":"Folder3"}
]

i want this to be saved in a file locally and return this file when api is called.

Upvotes: 0

Views: 130

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

You could set the Content-Disposition header to attachment and provide a filename:

public HttpResponseMessage Get()
{
    var value = new[]
    {
        new { FName = "Folder1" },
        new { FName = "Folder2" },
        new { FName = "Folder3" },
    };
    var response = Request.CreateResponse(HttpStatusCode.OK, value, this.Configuration.Formatters.JsonFormatter);
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "foo.json"
    };

    return response;
}

Upvotes: 1

Related Questions