0

To send data as querystring in PostAsync Method, I am using following approach. but i am getting Inernal Server Error.

 HttpResponseMessage response; string stringContent = "{ 'request_key': 'ABCD1234', 'request_code': 'CODE', 'request_type':'ID_type' }"; using(var client = new HttpClient()) { client.BaseAddress = new Uri(SubscriptionUtility.GetConfiguration("BaseURI")); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(SubscriptionUtility.GetConfiguration("ContentType"))); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", SubscriptionUtility.GetConfiguration("BasicAuthentication")); response = await client.PostAsync(SubscriptionUtility.GetConfiguration("SubscriptionAPI"), stringContent, new JsonMediaTypeFormatter()); if(response.IsSuccessStatusCode) { var dataObjects = JsonConvert.DeserializeObject<List<TestClass>>(response.Content.ReadAsStringAsync().Result); //foreach(var d in dataObjects) { //} } } 

But When i send the request through fiddler, Its working fine. Here is my fiddler request

User-Agent: Fiddler

Content-Type: application/json; charset=utf-8

Host: testapi.com

Content-Length: 93

Authorization: Basic 12fbe6e1f63d832aa33232323

Post Data: { "request_key":"ABCD1234", "request_code":"CODE", "request_type":"ID_type" }

2
  • and what is the trace when not using fiddler? Commented Aug 15, 2016 at 15:37
  • fyi basic authorization would be Authorization: Basic 12fbe6e1f63d832aa33232323, not token Commented Aug 15, 2016 at 15:38

1 Answer 1

1

I have achieved the desire functionality using following approach Post Request

using(var client = new HttpClient()) { client.BaseAddress = new Uri(SubscriptionUtility.GetConfiguration("BaseURI")); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(SubscriptionUtility.GetConfiguration("ContentType"))); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", SubscriptionUtility.GetConfiguration("BasicAuthentication")); var values = new Dictionary<string, string> { { "request_key", "ABCD1234" }, { "request_code", "CODE" }, { "request_type", "ID_type" } }; var content = new FormUrlEncodedContent(values); var response = await client.PostAsync(SubscriptionUtility.GetConfiguration("SubscriptionAPI"), content); var responseString = await response.Content.ReadAsStringAsync(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.