Phorest REST API。 CSV导出作业

时间:2018-01-10 06:33:17

标签: c# json rest

我目前正在开发一个c#服务,它从Phorest API(https://developer.phorest.com/)获取数据并将其放入数据库中。 我是REST的新手,想知道如何在上面的链接中执行POST方法,该方法创建了一个CSV,因为它有一些与之一起发送的JSON主体。我也尝试使用高级REST客户端访问CSVExportJob,但不确定我是否正在做正确的事情。

我可以按如下方式执行GET命令。

 public ObjectMapper PullDataFromPhorest()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
        //client.DefaultRequestHeaders.Add("authorization","Basic xxxxxx");
        string formattedContent = "";

        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  
        if (response.IsSuccessStatusCode)
        {


            string content = response.Content.ReadAsStringAsync().Result;
            dataObject = JsonConvert.DeserializeObject<ObjectMapper>(content);
            for (int i = 0; i < dataObject._embedded.branches.Length; i++)
            {
                formattedContent += "Branch ID: " + dataObject._embedded.branches[i].branchId.ToString() + " Branch Name: " + dataObject._embedded.branches[i].name.ToString() + "\n\n\n";
            }
        }
        else
        {

        }
        return dataObject;
    }

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作来发布JSON数据:

var url = "http://some-url.com";

var post_data = new
{
    field1 = "field1",
    field2 = "field2"
};

var client = new HttpClient();
var json = new StringContent(JsonConvert.SerializeObject(post_data), Encoding.UTF8, "application/json");
var response = client.PostAsync(url, json).Result;

if (response.IsSuccessStatusCode)
{
    // All good
}

这使用Newtonsoft.Json库(https://www.newtonsoft.com/json