1
Hello! I was sending files doing POST
for Azure Devops (documentation) via Javascript (just to test some features):
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/octet-stream; charset=UTF-8');
request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
request.setRequestHeader('Accept', 'application/json');
request.send(file);
For security reasons I was migrating this to server-side methods, which I am using c#. I have reached this point:
//criando o request
WebRequest request = WebRequest.Create(url);
//headers
request.ContentType = "application/octet-stream; charset=UTF-8";
request.Headers.Add("Authorization", "Basic " + authorizationBasic);
request.Headers.Add("20", "application/json"); //20 = accept
request.Method = "POST";
request.ContentLength = 0;
//body
Stream dataStream = request.GetRequestStream();
dataStream = file.InputStream;
dataStream.Close();
//fazendo o request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// le o conteudo da resposta
using (Stream dataStreamResp = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStreamResp);
string responseFromServer = reader.ReadToEnd();
}
I’m already consuming the ok API. I receive in return status 200 and the url of the file I uploaded as expected, but when opening the url of the answer it is all blank, that is, it is not sending the file, only its name.
I know that according to the documentation, I need to put in body
one string
with the Stream para upload
, but I don’t know how to do it and I didn’t have much success also searching through the internet...
Thank you!
generally in your Datastream would use . write() to write the content to be uploaded
– Lucas Miranda