如何通过REST API发送CSV文件?

时间:2018-07-13 23:23:59

标签: c# rest api restsharp

我正在尝试通过C#中的REST API将CSV文件发送到服务器。我一直在研究,他们建议我使用RestSharp库。考虑到REST API的所有方法都必须在标头中包含带有通过请愿POST获得的令牌的授权参数。

1 个答案:

答案 0 :(得分:0)

此解决方案对我有用。

//得到炸鸡

 public string getToken(String sURL, String sUserName, String sPassword)
 {  String access_token = "";
     string clientId = "client";
     string clientSecret = "secret";
     string credentials = String.Format("{0}:{1}", clientId, clientSecret);
     RestClient restClient = new RestClient(sURL);
     RestRequest restRequest = new RestRequest("/oauth/token");
     restRequest.RequestFormat = DataFormat.Json;
     restRequest.Method = Method.POST;
     restRequest.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
     restRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
     restRequest.AddHeader("Accept", "application/json");
     restRequest.AddParameter("grant_type", "password");
     restRequest.AddParameter("username", sUserName);
     restRequest.AddParameter("password", sPassword);
     try {
         var response = restClient.Execute(restRequest);
         if (response.StatusCode == HttpStatusCode.BadRequest){
             dynamic objError = new ExpandoObject();
             objError = JsonConvert.DeserializeObject(response.Content);
             access_token = "";
             var error = objError.invalid_grant;
             var error_description = objError.error_description;
             MessageBox.Show(error + " - " + error_description, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         if (response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.OK){
             dynamic objRpta = new ExpandoObject();
             objRpta = JsonConvert.DeserializeObject(response.Content);
             access_token = objRpta.access_token;
             String token_type = objRpta.token_type;
             int expires_in = objRpta.expires_in;
         }
     }
     catch(IOException e){
         MessageBox.Show(e.ToString(), "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     return access_token;
}

//send CSV
public string sendDocumentoCSV(String strAccesoToken, String strRutaDocumento, String strURL)
{
    String strTicket = "";
    String code = "";
    RestClient restClient = new RestClient(strURL);
    RestRequest restRequest = new RestRequest("/v1/document");
    restRequest.RequestFormat = DataFormat.Json;
    restRequest.Method = Method.POST;

    restRequest.AddHeader("Authorization", "Bearer " + strAccesoToken);
    restRequest.AddHeader("Content-Type", "multipart/form-data");
    restRequest.AddHeader("Accept", "application/json");

    restRequest.AddFile("file", strRutaDocumento);

    var response = restClient.Execute(restRequest);

    dynamic objRptaTicket = new ExpandoObject();
    objRptaTicket = JsonConvert.DeserializeObject(response.Content);

    if (response.StatusCode == HttpStatusCode.OK){ 
        code = objRptaTicket.code;
        strTicket = objRptaTicket.description;
    }
    else{
        strEnvioDocCode = objRptaTicket.code;
        strEnvioDocTicket = objRptaTicket.description;
        MessageBox.Show(strEnvioDocCode + " " + strEnvioDocTicket, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    return strTicket;
}