RestSharp请求的错误消息

时间:2018-05-25 15:41:18

标签: c# rest httprequest restsharp

我收到此错误消息:

{
    "result":"failure",
    "message":"Identifier is invalid",
    "time":"2018-05-25 15:34:13",
    "type":"items"
}

CompletedOK

这是针对SureDone API的。我不确定这个错误消息具体指的是什么。我从excel文件中提取数据,我可能使用了错误的内容类型?这是我的代码:

private void btnLoad_Click(object sender, EventArgs e)
{
    string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtChooseFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
    OleDbConnection conn = new OleDbConnection(PathConn);

    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + txtLoad.Text + "$]", conn);
    DataTable dt = new DataTable();

    da.Fill(dt);

    dataGridView.DataSource = dt;

    String payload = "";
    int i = 1;

    //REST request
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    var client = new RestClient("https://api.suredone.com/v1/editor/items/add");
    var request = new RestRequest(Method.POST);
    request.AddHeader("x-auth-token", "{token}");
    request.AddHeader("x-auth-user", "{username}");
    //request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddHeader("Content-type", "multipart/form-data");

    foreach(DataRow row in dt.Rows)
    {
        Hashtable postData = new Hashtable();
        postData["identifier"] = "guid";
        postData["guid"] = row["sku"].ToString();
        postData["price"] = row["price"].ToString();
        postData["title"] = row["title"].ToString();
        postData["action"] = row["action"].ToString();
        postData["stock"] = row["stock"].ToString();
        postData["msrp"] = row["msrp"].ToString();
        postData["longDescription"] = row["longdescription"].ToString();
        postData["condition"] = row["condition"].ToString();
        postData["brand"] = row["brand"].ToString();
        postData["notes"] = row["notes"].ToString();

        String formBoundary = "undefined";

        foreach (DictionaryEntry entry in postData)
        {
            payload += "--" + formBoundary + "\r\n" + 
                "Content-Disposition: form-data; name=" + 
                    entry.Key.ToString() + "\r\n\r\n" + 
                    entry.Value.ToString() + "\r\n\r\n";
        }//foreach(DictionaryEntry)CLOSE

        i++; //incrementer
    }//foreach(row) CLOSE

    //REST response 
    request.AddParameter("multipart/form-data", payload, ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    txtResponse.Text = response.Content + response.ErrorMessage + response.ResponseStatus + response.StatusCode;

    txtProducts.Text = payload;
}//button load click close 

1 个答案:

答案 0 :(得分:0)

检查第28行:

postData["identifier"] = "guid"; 

我认为应该是

postData["identifier"] = row["guid"].ToString();

然后检查下一行。这看起来很奇怪:

postData["guid"] = row["sku"].ToString();

请求和excel文件的字段是否相同?