PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 19, 2022

[FIXED] How can I Post parameter in httpClient where in postman use param

 May 19, 2022     api, c#, httpclient, web-services     No comments   

Issue

I want to post my parameter with HttpClient . what is the Content in httpclient request body? I wrote it int the body part and when I run the project I get Error.enter image description here

my code is:

string baseAddress = "https://WebServiceAddress";
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpRequestMessage bodyRequest = new HttpRequestMessage(HttpMethod.Post, baseAddress)

bodyRequest.Content = new StringContent(JsonConvert.SerializeObject(Prescriptionrequest), Encoding.UTF8, "application/json");

var responseApi = Client.PostAsync(baseAddress, bodyRequest.Content, new System.Threading.CancellationToken(false));

what should be the format of bodyRequest.Content?


Solution

Use this code:

HttpClient client = new HttpClient();
var dictionary = new Dictionary<string, string> 
{    
    { "parameter0", "value0" },    
    { "parameter1", "value1" }
};    
var content = new FormUrlEncodedContent(dictionary);    
var response = await client.PostAsync("https://WebServiceAddress", content);    
var responseString = await response.Content.ReadAsStringAsync();


Answered By - payam
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing