none
TFS REST API Upload Attachment using System.Net.Http.HttpClient RRS feed

  • Question

  • Hi,

    I'm using TFS 2018 and wants to upload attachment using System.Net.Http.HttpClient. I don;t want to use  TFS Extended Client for webAPI (https://www.nuget.org/packages/Microsoft.TeamFoundationServer.ExtendedClient/). Following is the request from Fiddler that works, but if I remove Fiddler-Encoding it doesn't work. I've truncated body content to paste here. Here's the Link for TFS attachment upload documentation (https://docs.microsoft.com/en-us/rest/api/vsts/wit/attachments/create#upload_a_binary_file), but it's much helpful.

    var method = new HttpMethod(httpMethod); 
    var request = new HttpRequestMessage(method, targetUrl);
    var memoryStream = new MemoryStream(patchDocumentBytes);
    request.Content = new StreamContent(memoryStream);
    
    



    POST http://mysystem:8080/tfs/DefaultCollection/_apis/wit/attachments?uploadType=Simple&filename=1.png&api-version=1.0 HTTP/1.1
    Accept: application/json;
    Content-Type: application/json
    Content-Length: 93070
    Authorization: Basic SGVsbG9Xb3JsZA==
    Fiddler-Encoding: base64
    
    iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAAAXNSR0IAr


    Friday, April 20, 2018 4:32 PM

All replies

  • Hi Faisal,

    Where did it not work? Did you receive any error?

    While testing HttpClient, did you close Fiddler?

    In my option, I would suggest you try TFS Extended Client to upload the file, use Fiddler to capture the request, and compare the difference between the TFS Client and HttpClient.

    In addition, this is WCF developing forum, we are not familiar with TFS Rest Service, you may consider posting your issue in Stackover flow with tagging TFS.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 23, 2018 3:22 AM
  • Thanks for reply, I couldn't find any forum for TFS/TFS client related issues. And wanted to get help from msdn.

    It doesn't response any error, it uploads the file in corrupted form.

    With in fiddler I found that the difference is Fiddler-Encoding. I'm assuming it's fiddler related, When this encoding is removed the file gets corrupted. but when I include this encoding it works.

    Fiddler-Encoding: base64

    so may be I'm not using correct encoding, I have not including any kind of encoding from HttpClient, I'm sending MemoryStream inside StreamContent object.Shall I use ByteArrayContent?

    I'm using application/octet-stream by using the following.

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));
    

    Do I need to explicitly specify ContentType inside content?

    var memoryStream = new MemoryStream(patchDocumentBytes);
    patchValue = new StreamContent(memoryStream);
    patchValue.Headers.ContentType = new MediaTypeHeaderValue(mediaType);

    I really appreciate your help, thanks again for your response and help.

    Monday, April 23, 2018 12:00 PM
  • Here's request sent from browser (TFS web cleint) to TFS, captured in Fiddler. And It works. I think missing some encoding.

    POST http://mysystem:8080/tfs/DefaultCollection/_apis/wit/attachments?uploadType=Simple HTTP/1.1
    Host: faisal:8080
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
    Accept: application/json;api-version=1.0;excludeUrls=true
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Content-Type: application/json
    Content-Length: 93070
    Authorization: Basic ZmFpc2FsQGVkZXZ0ZWNoOmludGU2NkdSRUFU
    Fiddler-Encoding: base64
    
    iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAA

    Monday, April 23, 2018 12:45 PM
  • When I'm uploading text file it's adding some meta-data. and I think same might be happening with non-text files. and it's happening in both cases either i use StreamContent class or ByteArrayContent class. I'm stuck in it kindly help me

    Content-Type: text/plain;
    	name="build version.txt"
    Content-Transfer-Encoding: 7bit
    Content-Disposition;
    	filename="build version.txt"
    var patchDocumentBytes = patchDocument as Stream;
                            if (patchDocumentBytes != null)
                            {
                                if (patchDocumentBytes.CanSeek)
                                {
                                    patchDocumentBytes.Seek(0, SeekOrigin.Begin);
                                }
                                //patchValue = new ByteArrayContent(patchDocumentBytes);
                                //var serializedObject = JsonConvert.SerializeObject(patchDocument, ignoreNull);
                                //var memoryStream = new MemoryStream(patchDocumentBytes);
                                patchValue = new StreamContent(patchDocumentBytes);
                                //patchValue.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
                            }








    Monday, April 23, 2018 3:31 PM
  • Hi Faisal,

    Thanks for more information.

    Your issue is more related with TFS which we are not familiar with.

    I suggest you go to StackOver Flow with tagging tfs for help. TFS related issue is supported in StackOverFlow. There is no forum in MSDN to support it.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 24, 2018 1:29 AM
  • Thanks for your time and response :)
    Tuesday, April 24, 2018 9:27 AM