locked
HTTP code 400 Bad Request error when saving a work item with special characters in html field programatically RRS feed

  • Question

  • Hi,

    I am writing a migration tool in C# which converts the data from excel file and create TFS customized work items. The data in excel can contain the extended ASCII characters which needs to be in description (HTML) field in work item. But Save() function for that work item(containing the special characters in description) fails with Bad request from the server error. If I remove the special characters, then work item is being saved.

    The description with special characters is :

    "
    On 15-10-01 @ 19:34 hrs, the application log recorded a warning for RCSManager on EH-RCS-MA

    "" 0›8›8›@›@›\ ""

    thses ASCII characters follow the text "" unable to distribute "" which occurrs directly after  "" unhandled exception "" warning

    note : the same string of warning errors was recorded on 16-10-01 @ 06:38:21  "

    Following is the stack trace:

     at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.ReadResponse(HttpWebResponse webResponse, WebException webException)
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.IsAuthenticationChallenge(TfsMessage requestMessage, HttpWebResponse webResponse, WebException webException, TfsMessage& responseMessage)
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequest()
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage message, TimeSpan timeout)
       at Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation operation, Object[] parameters, TimeSpan timeout, Object[]& outputs)
       at Microsoft.TeamFoundation.WorkItemTracking.Proxy.ClientService.Update(XmlNode package, XmlNode& result, MetadataTableHaveEntry[] metadataHave, String& dbStamp, RowSetCollection& metadata)
       at Microsoft.TeamFoundation.WorkItemTracking.Proxy.WorkItemServer.Update(String requestId, XmlElement package, XmlElement& result, MetadataTableHaveEntry[] metadataHave, String& dbStamp, IMetadataRowSets& metadata)
       at Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore.SendUpdatePackage(XmlElement package, XmlElement& result, Boolean bulk)
       at Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore.SaveWorkItems(WorkItem[] workitems, Boolean bulk, SaveFlags saveFlags)
       at Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem.Save(SaveFlags saveFlags)
       at Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem.Save()
       at SMTToTFSMigrationTool.SelectSMTData.CreateTFSWorkItem(Range row) in D:\Q113\SMTToTFSMigrationTool\SMTToTFSMigrationTool\SelectSMTData.cs:line 185

    Could you please help in finding the workaround of saving a work item with special characters.

    Thanks and Regards,

    Sonia


    • Edited by matkajeeta Monday, March 7, 2016 10:39 PM
    Monday, March 7, 2016 10:32 PM

All replies

  • Hi Sonia,  

    Thanks for your post.

    Please refer to the solution in this similar post: https://social.msdn.microsoft.com/Forums/vstudio/en-US/46446788-9dfd-4ed2-a376-0203d07089a5/the-request-was-rejected-by-the-serverrntechnical-informationrn-http-code-400-bad-request.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 8, 2016 6:44 AM
    Moderator
  • Hi John,

    Thanks for your reply.

    I did try the solution as suggested in the link. But for me it does not work. I am still getting the same error. I have tried both Unicode and ASCII coding.

    System.Text.Encoding unicode = System.Text.Encoding.ASCII;
    Byte[] unicodeText = unicode.GetBytes(field.ToString());
    MemoryStream memStream = new MemoryStream(unicodeText);
    StreamReader stream = new StreamReader(memStream);
    string text = stream.ReadToEnd();
    
     
    if (ffWI != null)
    {
     ffWI.Fields["ALMTool.Fault.Description"].Value = text;
    }

    the field is an object and contains the data as mentioned in my original post.

    Thanks

    Sonia

    Tuesday, March 8, 2016 9:54 PM
  • Hi Sonia,  

    Thanks for your reply.

    I created a C:\test.txt file within your same special characters, and run below code snippet to read content from this test.txt file and save it to work item’s Description field, it works fine.

                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("collectionURL"));

                tfs.EnsureAuthenticated();

                WorkItemStore workitemstore = tfs.GetService<WorkItemStore>();

                WorkItem wr = workitemstore.GetWorkItem(7744);

                StreamReader objReader = new StreamReader((System.IO.Stream)File.OpenRead(@"C:\test.txt"), System.Text.Encoding.Unicode);

                String str = objReader.ReadLine();

                wr.Fields["Description"].Value = str;

                wr.Save();


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 9, 2016 6:27 AM
    Moderator
  • Hi John,

    Really appreciate your help. But I tried doing exactly same. The work item was saved without error but the description field data was encrypted.

    ഢ伊㔱ㄭⴰ㄰䀠ㄠ㨹㐳栠獲‬桴⁥灡汰捩瑡潩潬⁧敲潣摲摥愠眠牡楮杮映牯删千慍慮敧⁲湯䔠ⵈ䍒ⵓ䅍਍਍∢〠鬐㠈鬐㠈鬐䀈鬐䀈鬐專∠ഢഊ琊獨獥䄠䍓䥉挠慨慲瑣牥⁳潦汬睯琠敨琠硥⁴∢甠慮汢⁥潴搠獩牴扩瑵⁥∢眠楨档漠捣牵獲搠物捥汴⁹晡整⁲∠•湵慨摮敬⁤硥散瑰潩∢眠牡楮杮਍਍潮整㨠琠敨猠浡⁥瑳楲杮漠⁦慷湲湩⁧牥潲獲眠獡爠捥牯敤⁤湯ㄠⴶ〱〭‱⁀㘰㌺㨸ㄲ†ഢ

     StreamReader stream = new StreamReader((System.IO.Stream)File.OpenRead(@"D:\SMT\testData.txt"), System.Text.Encoding.Unicode);
    string text = stream.ReadLine();
    
    if (ffWI != null)
    {
        ffWI.Fields["ALMTool.Fault.Description"].Value = text;
    }
    else if (sfWI != null)
    {
        sfWI.Fields["ALMTool.Fault.Description"].Value = text;
    }

    I guess I am missing some information here. The field type is HTML.

    Thanks

    Sonia

    Wednesday, March 9, 2016 7:41 PM
  • Hi Sonia,

    According to your description, you saved that text to the custom field, what’s the result if you specify that test to Description field (build-in field)?

    How do you custom the field? Please provide the detail information and steps.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 10, 2016 9:37 AM
    Moderator
  • Hi Starain,

    I modified the custom field to buit-in field (Description) and imported the new type definition.

    Same changes I applied to my code, but I am getting the same results with System.Description as well, which is encrypted data.

    To customize the field :

    1. In the type definition, I added a field with name, ref name and type as HTML.
    2. Added a control on layout and selected the field name as the new created field.
    3. Imported the type definition.

    I would have attached the screen shot, but my account is restricted for that.

    Thanks and Regards,

    Sonia

    Thursday, March 10, 2016 9:22 PM
  • Hi Sonia,

    What’s the result if you specify that text to description through UI in VS, then save it?

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 11, 2016 8:59 AM
    Moderator
  • You need to know the encoding of the file in order to allow the OpenRead to supply the right encoding. It clearly isn't unicode to begin with, nor is it ASCII, which encoding it is, the people creating the file can probably tell.

    And then before setting the field use the WebUtility.HtmlEncode method to ensure that your characters are preserved.


    My blog: blog.jessehouwing.nl

    Friday, March 11, 2016 2:47 PM
  • Hi Starain,

    From Team Explorer, If I create a work item and copy paste the description, I can save it and description remains same. Its just programmatically saving work item which is causing an issue.

    Regards,

    Sonia

    Sunday, March 13, 2016 8:25 PM
  • Hi Jesse,

    Actually the data which I am reading is being exported from another tool to excel file and as far as I am aware it can contain extended ASCII characters.

    I have tried reading the stream with all available encoding. But none is working.

    Regards,

    Sonia 

    Sunday, March 13, 2016 8:30 PM
  • Hi Sonia,

    What’s the version of assembly files that you used? I am using 14.0 and can’t reproduce that issue.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 14, 2016 10:13 AM
    Moderator