Skip to content

Instantly share code, notes, and snippets.

View oliveira-michel's full-sized avatar
🏠
Working from home

oliveira-michel

🏠
Working from home
View GitHub Profile
@oliveira-michel
oliveira-michel / orders\view.py
Last active June 27, 2022 16:45
How to set and get Path / Route / URL or Query parameters in Django
class Orders(View):
def get(self, request, *args, **kwargs):
...
id=request.GET.get("id"),
@oliveira-michel
oliveira-michel / PostAndGetFile.cs
Created March 18, 2020 21:09
Exemplo de cliente enviando um arquivo via HTTP via multipart e recebendo outro de volta
public void Teste()
{
var retorno = PostAndGetArquivo();
byte[] bytesImagem;
using (MemoryStream ms = new MemoryStream())
{
retorno.CopyTo(ms);
bytesImagem = ms.ToArray();
@oliveira-michel
oliveira-michel / GetFile.cs
Created March 18, 2020 20:31
Exemplo de Cliente consumindo arquivo via HTTP
public void Teste()
{
var retorno = GetArquivo();
byte[] bytesImagem;
using (MemoryStream ms = new MemoryStream())
{
retorno.CopyTo(ms);
bytesImagem = ms.ToArray();
}
File.WriteAllBytes($"c:\\teste.png", bytesImagem);
@oliveira-michel
oliveira-michel / ExemploAcceptController.cs
Created March 10, 2020 14:23
Exemplo de consulta com Accepts diferentes (binario x json)
[HttpGet("contas/{idConta}/extrato")]
[Produces("application/json", "application/pdf")]
public ActionResult<ExtratoModel> GetExtrato([FromHeader] string accept, [FromQuery] string fromData)
{
string[] acceptedTypes = accept.Split(',')
.Select(type => MediaTypeHeaderValue.Parse(type))
.OrderByDescending(type => type.Parameters.FirstOrDefault(p => p.Name == "q")?.Value)
.Select(type => type.ToString()).ToArray();
foreach (string acceptedType in acceptedTypes)
@oliveira-michel
oliveira-michel / ExemploMultipartFormDataController.cs
Created March 6, 2020 19:07
Exemplo de Controller em .NET Core que recebe multipart/form-data
[HttpPost("arquivos")]
public ActionResult Student([FromForm]ArquivoModel arquivo)
{
string descricao = arquivo.Descricao;
string autor = arquivo.Autor;
var file = arquivo.Arquivo;
if (file.Length > 0) {
using (var fileStream = new FileStream(file.FileName, FileMode.Create)) {