0

How to add the below headers to httpclient .NET 4.7.2. and How to add this json to the body of a PUT request?

I did not create the server I put test to sensor the data its a third party plugin. Using the header setting and the Json body in Postman client app with the Postman Agent app this works. But it does not work when trying through C# I am getting Error: 400 Bad request.

Cache-Control -> no-cache
Postman-Token -> <calculated when request is sent>
Content-Type  -> application/json
Content-Length -> <calculated when request is sent>
Host -> <calculated when request is sent>
User-Agent -> Postman Runtime/7.29.0
Accept -> */*
Accept-Encoding -> gzip,deflate,br

Json:

{
    "objectPath": "/TestData1/TestData2/_C++TestData3/TestData6.TestData8:TestData15",
    "functionName": "Test_FuncCall",
    "generateTransaction": true
}

Code:

static void Main(string[] args)
{
    using (var client = new HttpClient())
    {
        JObject data = JObject.Parse(File.ReadAllText(@"D:\data.txt"));


        client.BaseAddress = new Uri("http://127.0.0.1:5555/");

        var content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");


        var response = client.PutAsync("test1/test2/test3", content).Result;

        if (response.IsSuccessStatusCode)
        {
            Console.Write("Success");
        }
        else
            Console.Write("Error");
    }
    Console.ReadKey();
}

One step after break point then hover over response:

{StatusCode: 400, ReasonPhrase: '', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS
  Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
  Access-Control-Max-Age: 600
  keep-alive: timeout=15.000000
  Content-Length: 67
  Content-Type: application/json
}}

This Works:

Call:

  TestServer yyy = new TestServer();
            JObject data2 = JObject.Parse(File.ReadAllText(@"D:\data.txt"));
            yyy.SendRequest("http://127.0.0.1:5555/test1/test2/test3", "PUT", data2.ToString(), false);

function:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURIString);
request.UserAgent = "Test App";
request.Method = method;

if (body != "")
{
     p_CurrentAPIRequest = body;
    request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
else
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);

allDone.WaitOne();
4
  • Are you getting the correct JSON structure in your content variable? You can add your headers with your request like this: client.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); and so on. Commented Mar 2, 2022 at 5:27
  • @RahulSharma Yes I, Copied the json directly from the Postman Body. Commented Mar 2, 2022 at 8:46
  • Okay, did you try adding the request headers ? Also what response are you getting on reponse? Commented Mar 2, 2022 at 8:51
  • @RahulSharma addes response to question edit above Commented Mar 2, 2022 at 9:00

2 Answers 2

3

The simplest way to get web request code from Postman is to use its Code feature. enter image description here

And then select RestSharp from the dropdown, enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You can use DefaultRequestHeaders.Add() method in the following way:

HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.Add("ApiKey", key);
HttpClient.DefaultRequestHeaders.Add("Authorization", String.Format("Bearer {0}", token));

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.