Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enviar POST desde C# a REST API
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace APIronga
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
String Nombre = txt2.Text;
String User = txt3.Text;
String Email = txt4.Text;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://jsonplaceholder.typicode.com/users");
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.Accept = "application/json; charset=utf-8";
using (var streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
{
string loginjson = JsonConvert.SerializeObject(new
{
name = Nombre,
email = Email
});
streamWriter.Write(loginjson);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
MessageBox.Show(result.ToString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.