简体   繁体   中英

Error message for RestSharp request

I'm receiving this error message:

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

CompletedOK

This is going out to the SureDone API. I'm not sure what this error message is referring to specifically. I'm pulling in the data from an excel file, am I maybe using the wrong content-type? Here's my code:

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 

Check line 28:

postData["identifier"] = "guid"; 

I think it should be

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

And check next line. It seems strange:

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

Are the fields of the request and excel file identical?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM