How to call Rest API with Content and Headers in c#?RestSharp post request - Body with x-www-form-urlencoded valuesHow do I calculate someone's age in C#?Best Practices for securing a REST API / web serviceCalling the base constructor in C#How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?How do you set the Content-Type header for an HttpClient request?C# Console Rest API header payload
What happens if you do emergency landing on a US base in middle of the ocean?
What happens to foam insulation board after you pour concrete slab?
What is the traditional way of earning a doctorate in Germany?
Pay as you go Or Oyster card
Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP)/sleep apnea device?
How to generate random points without duplication?
How to supress loops in a digraph?
How were concentration and extermination camp guards recruited?
Movie where a boy is transported into the future by an alien spaceship
What are the words for people who cause trouble believing they know better?
C SIGINT signal in Linux
How do I write "Show, Don't Tell" as an Asperger?
Fair use: Instructor sharing notes from textbooks
What happened to all the nuclear material being smuggled after the fall of the USSR?
PhD student with mental health issues and bad performance
Can a 2nd-level sorcerer use sorcery points to create a 2nd-level spell slot?
Ancestor born in Bristol City workhouse?
Are there cubesats in GEO?
How to make thick Asian sauces?
Efficiently merge lists chronologically without duplicates?
How to make a setting relevant?
Does the growth of home value benefit from compound interest?
Pronoun introduced before its antecedent
Short story written from alien perspective with this line: "It's too bright to look at, so they don't"
How to call Rest API with Content and Headers in c#?
RestSharp post request - Body with x-www-form-urlencoded valuesHow do I calculate someone's age in C#?Best Practices for securing a REST API / web serviceCalling the base constructor in C#How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?How do you set the Content-Type header for an HttpClient request?C# Console Rest API header payload
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to call Rest Api with content and headers in c#. Actully I am trying to convert to c# from Pyhton code which is:
import requests
url = 'http://url.../token'
payload = 'grant_type=password&username=username&password=password'
headers =
'Content-Type': 'application/x-www-form-urlencoded'
response = requests.request('POST', url, headers = headers, data = payload, allow_redirects=False)
print(response.text)
So far I am trying with:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Url);
var tmp = new HttpRequestMessage
Method = HttpMethod.Post,
Content =
;
var result = client.PostAsync(Url, tmp.Content).Result;
}
I have no idea how to put from Pyhton code Headers (Content-Type) and additioanl string (payload).
c# rest
add a comment |
I am trying to call Rest Api with content and headers in c#. Actully I am trying to convert to c# from Pyhton code which is:
import requests
url = 'http://url.../token'
payload = 'grant_type=password&username=username&password=password'
headers =
'Content-Type': 'application/x-www-form-urlencoded'
response = requests.request('POST', url, headers = headers, data = payload, allow_redirects=False)
print(response.text)
So far I am trying with:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Url);
var tmp = new HttpRequestMessage
Method = HttpMethod.Post,
Content =
;
var result = client.PostAsync(Url, tmp.Content).Result;
}
I have no idea how to put from Pyhton code Headers (Content-Type) and additioanl string (payload).
c# rest
I gave you an answer based on HttpClient. Please check.
– Darkonekt
Mar 25 at 4:39
add a comment |
I am trying to call Rest Api with content and headers in c#. Actully I am trying to convert to c# from Pyhton code which is:
import requests
url = 'http://url.../token'
payload = 'grant_type=password&username=username&password=password'
headers =
'Content-Type': 'application/x-www-form-urlencoded'
response = requests.request('POST', url, headers = headers, data = payload, allow_redirects=False)
print(response.text)
So far I am trying with:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Url);
var tmp = new HttpRequestMessage
Method = HttpMethod.Post,
Content =
;
var result = client.PostAsync(Url, tmp.Content).Result;
}
I have no idea how to put from Pyhton code Headers (Content-Type) and additioanl string (payload).
c# rest
I am trying to call Rest Api with content and headers in c#. Actully I am trying to convert to c# from Pyhton code which is:
import requests
url = 'http://url.../token'
payload = 'grant_type=password&username=username&password=password'
headers =
'Content-Type': 'application/x-www-form-urlencoded'
response = requests.request('POST', url, headers = headers, data = payload, allow_redirects=False)
print(response.text)
So far I am trying with:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Url);
var tmp = new HttpRequestMessage
Method = HttpMethod.Post,
Content =
;
var result = client.PostAsync(Url, tmp.Content).Result;
}
I have no idea how to put from Pyhton code Headers (Content-Type) and additioanl string (payload).
c# rest
c# rest
asked Mar 24 at 14:20
DaniKRDaniKR
69152138
69152138
I gave you an answer based on HttpClient. Please check.
– Darkonekt
Mar 25 at 4:39
add a comment |
I gave you an answer based on HttpClient. Please check.
– Darkonekt
Mar 25 at 4:39
I gave you an answer based on HttpClient. Please check.
– Darkonekt
Mar 25 at 4:39
I gave you an answer based on HttpClient. Please check.
– Darkonekt
Mar 25 at 4:39
add a comment |
3 Answers
3
active
oldest
votes
Here a sample I use in one of my apps:
_client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
add a comment |
If you use RestSharp, you should be able to call your service with the following code snipped
var client = new RestClient("http://url.../token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=username&password=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
I based my answer on the anwser of this answer.
add a comment |
using System.Net.Http;
var content = new StringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
Or use FormUrlEncodedContent
without set header
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
If you write UWP application, use HttpStringContent
or HttpFormUrlEncodedContent
instead in Windows.Web.Http.dll.
using Windows.Web.Http;
var content = new HttpStringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
1
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324755%2fhow-to-call-rest-api-with-content-and-headers-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here a sample I use in one of my apps:
_client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
add a comment |
Here a sample I use in one of my apps:
_client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
add a comment |
Here a sample I use in one of my apps:
_client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
Here a sample I use in one of my apps:
_client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
edited Mar 25 at 14:00
answered Mar 25 at 4:38
DarkonektDarkonekt
1,10911518
1,10911518
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
add a comment |
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
Yes something like this, how did you solve setting like timeout=undefined and allow_redirects=False
– DaniKR
Mar 25 at 8:22
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
@DaniKR like this: _client = new HttpClient BaseAddress = new Uri(ConfigManager.Api.BaseUrl), Timeout = new TimeSpan(0, 0, 0, 0, -1) ;
– Darkonekt
Mar 25 at 14:00
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
Just specify the timeout you want
– Darkonekt
Mar 25 at 14:01
add a comment |
If you use RestSharp, you should be able to call your service with the following code snipped
var client = new RestClient("http://url.../token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=username&password=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
I based my answer on the anwser of this answer.
add a comment |
If you use RestSharp, you should be able to call your service with the following code snipped
var client = new RestClient("http://url.../token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=username&password=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
I based my answer on the anwser of this answer.
add a comment |
If you use RestSharp, you should be able to call your service with the following code snipped
var client = new RestClient("http://url.../token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=username&password=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
I based my answer on the anwser of this answer.
If you use RestSharp, you should be able to call your service with the following code snipped
var client = new RestClient("http://url.../token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=username&password=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
I based my answer on the anwser of this answer.
answered Mar 24 at 14:33
Volkmar RigoVolkmar Rigo
6741129
6741129
add a comment |
add a comment |
using System.Net.Http;
var content = new StringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
Or use FormUrlEncodedContent
without set header
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
If you write UWP application, use HttpStringContent
or HttpFormUrlEncodedContent
instead in Windows.Web.Http.dll.
using Windows.Web.Http;
var content = new HttpStringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
1
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
add a comment |
using System.Net.Http;
var content = new StringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
Or use FormUrlEncodedContent
without set header
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
If you write UWP application, use HttpStringContent
or HttpFormUrlEncodedContent
instead in Windows.Web.Http.dll.
using Windows.Web.Http;
var content = new HttpStringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
1
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
add a comment |
using System.Net.Http;
var content = new StringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
Or use FormUrlEncodedContent
without set header
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
If you write UWP application, use HttpStringContent
or HttpFormUrlEncodedContent
instead in Windows.Web.Http.dll.
using Windows.Web.Http;
var content = new HttpStringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
using System.Net.Http;
var content = new StringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
Or use FormUrlEncodedContent
without set header
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
If you write UWP application, use HttpStringContent
or HttpFormUrlEncodedContent
instead in Windows.Web.Http.dll.
using Windows.Web.Http;
var content = new HttpStringContent("grant_type=password&username=username&password=password");
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.PostAsync(Url, content);
var data = new Dictionary<string, string>
"grant_type", "password",
"username", "username",
"password", "password"
;
var content = new FormUrlEncodedContent(data);
client.PostAsync(Url, content);
edited Mar 25 at 3:14
answered Mar 24 at 14:35
shingoshingo
3,9303823
3,9303823
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
1
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
add a comment |
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
1
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
What reference I have to use for "HttpFormUrlEncodeContent"?
– DaniKR
Mar 24 at 14:51
1
1
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
Windows.Web.Http.dll. I've also add code for normal .net api.
– shingo
Mar 25 at 3:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324755%2fhow-to-call-rest-api-with-content-and-headers-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I gave you an answer based on HttpClient. Please check.
– Darkonekt
Mar 25 at 4:39