Convert CURL to C# using HttpClient - c#

I am trying to convert the following CURL command:
curl --request POST --url http://localhost:8042/tools/find --data "{\"Level\":\"Study\",\"Query\":{\"Modality\":\"MR\",\"StudyInstanceUID\":\"*\"}}"
to a C# rest client method, using HttpClient without success.
One approach i have been trying is the following:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:8042");
string test = "{\"Level\":\"Study\",\"Query\":{\"Modality\":\"MR\",\"StudyInstanceUID\":\"*\"}}";
var requestContent = new StringContent(test, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync("tools/find", requestContent);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
// Write the output.
Console.WriteLine(await reader.ReadToEndAsync());
return await reader.ReadToEndAsync();
}
}
But either i am getting some timeout exceptions or the returned result in NULL.
This is what i am getting when i run the CURL command:
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8042 (#0)
> POST /tools/find HTTP/1.1
> Host: localhost:8042
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Length: 66
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 66 out of 66 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/json; charset=utf-8
< Content-Length: 1023
<
[
"06a5bb05-acf327ff-1b1f7432-543a3572-d0778630",
"0d2858ee-9bb9557f-6779c861-4e55604a-bbd9d561",
"241d9063-668f6d5a-84d5e791-aae25988-cbe330e4",
"2a47771d-a7fed498-2ea74733-6ba5b408-0af517d0",
"2c22461c-2529103d-1c3bbf0e-5c1011b7-ef4c4702",
"2e89270f-77b2368b-ec8f7a47-48922528-6d82a563",
"46151c0f-a92e4ffe-b3964a0b-0a217ff0-e138a9b0",
"4ac07d24-df6720d8-410ded38-80c42f81-029b826d",
"6ae5803f-564d02d6-ce2d03c9-87029ebb-c5f5b783",
"6c4dd689-5dc50cc7-7c0b07e1-231c8f06-10a50343",
"79a0e646-d244dced-2a2ac6d0-e61e6029-38b1e61e",
"7beb9698-c3e13f1a-5449e8c0-06f61be7-0285b222",
"8c35bbfa-1f00d0bb-50fdddea-c8b8f085-20ef243a",
"9a318c16-75a5cae8-3f42dd60-2ab5d0c0-664e78d1",
"a7b43909-4ecfe2fe-12f414ad-dc1d013e-0665e60b",
"b85380e8-e66db7da-d575e3d3-80bce548-71d5c251",
"d07b73c3-77cad4ff-1bf045d9-d8677f33-cd4c79f5",
"ec81b8b0-c6f95b97-2a2299ca-fa4d3f68-d79f0079",
"ec9c971a-e66c350c-f808f182-7716b99c-4c8f6f86",
"efe910b9-3bb0e298-c9ec181b-3985c6be-a6b74a89"
]
* Connection #0 to host localhost left intact
Any ideas about how can i convert this command to working c# code?

don't repeat the full URL in PostAsync if you are setting BaseAddress
using (var client = new HttpClient()){
client.BaseAddress = new Uri("http://localhost:8042");
...
var response = await client.PostAsync("tools/find", requestContent);
...
}

After some research and testing here is the code to make a corresponding to curl:
curl --request POST --url http://localhost:8042/tools/find --data "{\"Level\":\"Study\",\"Query\":{\"Modality\":\"MR\",\"StudyInstanceUID\":\"*\"}}"
post request in C#:
public async System.Threading.Tasks.Task<string> MyMethod(string studyInstanceUID)
{
using (var client = new HttpClient())
{
string data = "{\"Level\":\"Study\",\"Query\":{\"Modality\":\"MR\",\"StudyInstanceUID\":\"" + studyInstanceUID + "\"}}";
var requestContent = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
client.DefaultRequestHeaders.Add("User-Agent", "Anything");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (reqAuth)
{
var byteArray = Encoding.ASCII.GetBytes(authString);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
}
HttpResponseMessage response = await client.PostAsync(baseUrl + "tools/find", requestContent);
var responseContent = response.Content;
return responseContent.ReadAsStringAsync().Result;
}
}

If you don't mind using a small wrapper library, Flurl (disclaimer: I'm the author), which wraps HttpClient and Json.NET, makes this about as simple as using cURL.
using Flurl.Http;
var results = await "http://localhost:8042/tools/find"
.PostJsonAsync(new { Level = "Study", Query = new { Modality = "MR", StudyInstanceUID = "*" }})
.ReceiveJson<string[]>();
Note that JSON serializaiton is handled implicitly, so you're only ever dealing with strongly typed C# objects, rather than stringifying/parsing the JSON that goes in and comes out.

Related

Remove headers from HttpClient response

I'm facing a problem when using HttpClient. The call works right and I get an answer but I can't get the content properly.
The function I wrote looks like this:
public async Task<string> MakePostRequestAsync(string url, string data, CancellationToken cancel)
{
String res = String.Empty;
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpContent content = new StringContent(data, Encoding.UTF8, "application/xml");
httpClient.DefaultRequestHeaders.Authorization = getHeaders();
httpClient.DefaultRequestHeaders.Add("Accept", "application/xml");
httpClient.DefaultRequestHeaders.Add("User-Agent", "C#-AppNSP");
httpClient.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await httpClient.PostAsync(url, content, cancel);
response.EnsureSuccessStatusCode(); // Lanza excepción si no hay éxito
res = await response.Content.ReadAsStringAsync();
if (String.IsNullOrEmpty(res))
{
throw new Exception("Error: " + response.StatusCode);
}
}
return res;
}
The response string I get is similar to this one:
HTTP/1.1 0 nullContent-Type: application/xml;charset=UTF-8
Content-Length: 1263
Date: Tue, 02 Jul 2019 07:48:07 GMT
Connection: close
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SeguimientoEnviosFechasResponse xsi:noNamespaceSchemaLocation="SeguimientoEnviosFechasResponse.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Error>0</Error>
<MensajeError></MensajeError>
<SeguimientoEnvioFecha>
<!-- more XML here -->
</SeguimientoEnvioFecha>
</SeguimientoEnviosFechasResponse>
This string includes headers for some reason, so when I try to deserialize it I get an error.
How can I remove this headers in the response string?
Your server returns headers in the response body. Would be good to fix this on a server side, if it's not possible you should extract body from the response:
var xml = res.Substring(res.IndexOf("<?xml", StringComparison.Ordinal));
You may try this:
using (var receiveStream = response.GetResponseStream())
{
using (var readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
Console.WriteLine (readStream.ReadToEnd ());
}
}

How do you add this big header in POST request in C#?

I've found quite a lot of answers with regards to my question yet I am sure that I am doing something wrong because none really works and I just want to pass my own cookie session as well as the token and some other information from header when sending a POST request to server. This is being executed when button is pressed on the application:
private void button1_Click(object sender, EventArgs e)
{
PostRequest("https://www.example.com");
}
--
async static void PostRequest(string url)
{
//PASS HEADER HERE
//POST DATA
IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("data1": "name"),
new keyvaluepair<string, string>("data2": "555")
};
HttpContent q = new FormUrlEncodedContent(queries);
using (HttpClient client = new HttpClient()) //using for disposing if not in use
{
using (HttpResponseMessage response = await client.PostAsync(url, q)) //url, content in postAsync
{
using (HttpContent content = response.Content) //store content (data) into a 'content'
{
string mycontent = await content.ReadAsStringAsync();
//HttpContentHeaders headers = content.Headers;
Console.WriteLine(mycontent);
}
}
}
}
--
This is the cookie I want to pass and and the token along with other info in header:
POST /test/data HTTP/1.1
Host: host.datanet.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Accept: */*
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
X-XSRF-TOKEN: eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJRUUrRUl0NlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0=
Referer: https://refererwebsitetest.html
Content-Length: 61
Cookie: session=eyJpdiI6IkI4XC9ndnFr6UlwvbEZMWhIK1wvbTFRSkE9PSIsInZhbHVlIjoicStyR2c3T240UW41RW5OdEFtcM2NjMjJ2V2d3Q0Yk9cL0tjZEFyV1NwSmpDbmdyc3BGZklva3RhaFJ5SWFCOFdiOHhobE1ySlU3NnJhMXMrckMzeXkxOiI1MjQxMkZuTndnPT0iLCJtYWMiY2VjMDVmiODE4YjI2YzViMjdiYzQ4MTk1ZDllMDVjOGE0MGRkMTFiYWFkNDRlZjM1MGQ4YzZjIn0%3D; XSRF-TOKEN=eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0%3D
Connection: keep-alive
I am really a beginner in C# and would appreciate any help.
Thank you!
Updated code:
async static void PostRequest(string url)
{
//PASS HEADER DATA
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
//Repeat for each header you want
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
request.Headers.Add("Content-Type", "application/json");
request.Headers.Add("X-XSRF-TOKEN", "eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJRUUrRUl0NlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0=");
request.Headers.Add("Referer", "https://refererwebsitetest.html");
request.Headers.Add("Content-Length", "61");
request.Headers.Add("Cookie", "session=eyJpdiI6IkI4XC9ndnFr6UlwvbEZMWhIK1wvbTFRSkE9PSIsInZhbHVlIjoicStyR2c3T240UW41RW5OdEFtcM2NjMjJ2V2d3Q0Yk9cL0tjZEFyV1NwSmpDbmdyc3BGZklva3RhaFJ5SWFCOFdiOHhobE1ySlU3NnJhMXMrckMzeXkxOiI1MjQxMkZuTndnPT0iLCJtYWMiY2VjMDVmiODE4YjI2YzViMjdiYzQ4MTk1ZDllMDVjOGE0MGRkMTFiYWFkNDRlZjM1MGQ4YzZjIn0%3D; XSRF-TOKEN=eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0%3D");
request.Headers.Add("Connection", "keep-alive");
using (HttpClient client = new HttpClient())
{
await client.SendAsync(request);
}
//POST DATA
IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("data1": "name"),
new KeyValuePair<string, string>("data2": "555")
};
HttpContent q = new FormUrlEncodedContent(queries);
using (HttpClient client = new HttpClient()) //using for disposing if not in use
{
using (HttpResponseMessage response = await client.PostAsync(url, q)) //url, content in postAsync
{
using (HttpContent content = response.Content) //store content (data) into a 'content'
{
string mycontent = await content.ReadAsStringAsync();
//HttpContentHeaders headers = content.Headers;
Console.WriteLine(mycontent);
}
}
}
}
it comes up with the following errors:
// FIXED
applied the code from Microsoft website:
https://learn.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class
lastly added headers from your example, THX works!
You can use the HttpRequestMessage class and set the headers you want then send it as a POST using the SendAsync method on the HttpClient.
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
//Repeat for each header you want
request.Headers.Add("headerName","headervalue");
using (HttpClient client = new HttpClient())
{
await client.SendAsync(request);
}

Equivalent of "curl -F" parameter for System.Net.Http.MultipartFormDataContent?

I am trying to use the sonicAPI file/upload API in C#.
My attempt to translate the curl example to C# with HttpClient and MultipartFormDataContent returns the error 400 / Bad Request.
Content of the response :
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status code="400" />
<errors>
<error message="File upload failed: file is missing." parameter="file" error_code="10" />
</errors>
</response>
Example of the curl command-line shown in the documentation :
curl https://api.sonicapi.com/file/upload?access_id=$ACCESS_ID -Ffile=#Vocals.mp3
Code I've crafted so far :
public async Task<HttpResponseMessage> Post(string id, string fileName)
{
string url = string.Format("http://api.sonicapi.com/file/upload?access_id={0}", id);
var stream = new FileStream(fileName, FileMode.Open);
var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };
var content = new MultipartFormDataContent();
content.Add(new StreamContent(stream), "file");
HttpResponseMessage message = await client.PostAsync(url, content);
string s = await message.Content.ReadAsStringAsync();
return message;
}
I've tried to remove "file" from content.Add(new StreamContent(stream), "file"); but it didn't help.
Note : the upload happens (i.e. it does not return immediately)
Do you know what is the equivalent of the curl -F parameter when using .NET web classes ?
EDIT :
Output of curl -v
* Hostname was NOT found in DNS cache
* Trying 87.106.252.119...
* Connected to api.sonicapi.com (87.106.252.119) port 80 (#0)
> POST /file/upload?access_id=xxxxxxxxxxxx HTTP/1.1
> User-Agent: curl/7.35.0
> Host: api.sonicapi.com
> Accept: */*
> Content-Length: 882266
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------b3c6dc0fc9
34fc71
>
< HTTP/1.1 100 Continue
< HTTP/1.1 201 Created
* Server nginx/0.7.67 is not blacklisted
< Server: nginx/0.7.67
< Date: Tue, 18 Feb 2014 21:14:09 GMT
< Content-Type: application/xml
< Connection: keep-alive
< X-Powered-By: Express
< X-Sonicapi-Request-Id: 6422cd9a-6069-4c2f-a3c5-0865c8ada6d5
< Access-Control-Allow-Origin: *
< location: /file/download?file_id=dae4e051-fe11-4058-a009-855dbb74de50
< X-Sonicapi-File-Id: dae4e051-fe11-4058-a009-855dbb74de50
< Content-Length: 249
<
<?xml version="1.0" encoding="utf-8"?><response><status code="201"/><file file_i
d="dae4e051-fe11-4058-a009-855dbb74de50" status="ready" href="/file/download?fil
e_id=dae4e051-fe11-4058-a009-855dbb74de50" remaining_lifetime_seconds="3599"/></
response>* Connection #0 to host api.sonicapi.com left intact
Output of the request using Fiddly :
POST http://api.sonicapi.com/file/upload?access_id=xxxxxxxx
HTTP/1.1
Content-Type: multipart/form-data; boundary="bd6fba7f-c173-4470-9c44-c9cc91f618a9"
Host: api.sonicapi.com
Content-Length: 882175
Expect: 100-continue
Connection: Keep-Alive
--bd6fba7f-c173-4470-9c44-c9cc91f618a9
Content-Disposition: form-data; name=file
RIFFvu
�WAVEfmt
(truncated)
Thanks to #bzlm, using Fiddler I managed to track what was missing :
Content disposition
Content type
And these needed to be set on streamContent rather than content.
public async Task<HttpResponseMessage> Post(string id, string fileName)
{
string url = string.Format("http://api.sonicapi.com/file/upload?access_id={0}", id);
var stream = new FileStream(fileName, FileMode.Open);
string name = Path.GetFileName(fileName);
var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };
var streamContent = new StreamContent(stream);
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
streamContent.Headers.ContentDisposition.Name = "\"file\"";
streamContent.Headers.ContentDisposition.FileName = "\"" + name + "\"";
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var content = new MultipartFormDataContent { streamContent };
HttpResponseMessage message = await client.PostAsync(url, content);
string s = await message.Content.ReadAsStringAsync();
return message;
}

Forcing HttpClient to use Content-Type: text/xml

This is driving me nuts, I am setting the ContentType header everywhere I can and can't seem to make it stop sending text/plain.
Watching the data in Fiddler, the request is always requesting:
POST http:/domain.com HTTP/1.1
Content-Type: text/plain; charset=utf-8
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
request.Content = new StringContent(Serialize(obj), Encoding.UTF8, "text/xml");
request.Content.Headers.Clear();
request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
request.Headers.Clear();
request.Headers.Add("Content-Type","text/xml");
var response = await httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
It looks like you tried to hard :) This should just work.
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
request.Content = new StringContent(Serialize(obj), Encoding.UTF8, "text/xml");
var response = await httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
Try settings the default request headers:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
Use "application/xml" instead of "text/xml"

json parameter to request on Windows 8 (RT)

I'm looking for solution to send request with JSON like parameter to server.
I use this code,
var httpClient = new HttpClient();
var tempByteArray = Encoding.UTF8.GetBytes("my valid json");
var stream = new MemoryStream(tempByteArray);
var streamContent = new StreamContent(stream);
var request = new HttpRequestMessage(HttpMethod.Post, Constants.LocalServer);
request.Content = streamContent;
request.Headers.TransferEncodingChunked = true;
HttpResponseMessage response = await httpClient.SendAsync(request);
But in response I get:
{
StatusCode: 501,
ReasonPhrase: 'NotImplemented',
Version: 1.0,
Content: System.Net.Http.StreamContent,
Headers: {
X-Squid-Error: ERR_UNSUP_REQ0X-Cache: MISSfromproxy3.itos.orgX-Cache-Lookup: NONEfromproxy3.companyname.org: portProxy-Connection: closeDate: Thu,
18Apr201309: 17: 53GMTServer: squid/2.6.STABLE21Via: 1.0proxy3.companyname.org: port(squid/2.6.STABLE21)Content-Length: 1099Content-Type: text/htmlExpires: Thu,
18Apr201309: 17: 53GMT
}
}
May be have another way to sent request with json parameter on Win8?
UPDATE I found solution:
public static async Task<string> LoadData(string json, string serverUrl)
{
var request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.LocalServer));
request.ContentType = "application/json";
request.Method = "POST";
using (var requestStream = await request.GetRequestStreamAsync())
{
var writer = new StreamWriter(requestStream);
writer.Write(json);
writer.Flush();
}
using (var resp = await request.GetResponseAsync())
{
using (var responseStream = resp.GetResponseStream())
{
var reader = new StreamReader(responseStream);
return = reader.ReadToEnd();
}
}
}
It's work great, but must exists more simple way(i hope). And I'll try to find it.
When I post data using your two code snippets, I see some differences in the requests.
Here is the raw post for the first code sample (that you say does not work):
POST http://testing.foo.com/api/Values HTTP/1.1
Host: testing.foo.com
Expect: 100-continue
Connection: Keep-Alive
Content-Length: 75
{
id:"1",
title:"title text",
post:"post text",
isDeleted:"False"
}
This is the raw post for the code in your update (code that you say works):
POST http://testing.foo.com/api/Values HTTP/1.1
Content-Type: application/json
Host: testing.foo.com
Content-Length: 75
Expect: 100-continue
{
id:"2",
title:"title text",
post:"post text",
isDeleted:"False"
}
The differences in the two requests are as follows:
In the first request, the content type is never set.
In the first request, the content is UTF8 encoded.
To fix your non-working code, I would suggest you try one or both of the following:
Set the content type to application/json
Not UTF8 encode the request
At that moment this solution is most useful.
public static async Task<string> LoadData(string json, string serverUrl)
{
var request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.LocalServer));
request.ContentType = "application/json";
request.Method = "POST";
using (var requestStream = await request.GetRequestStreamAsync())
{
var writer = new StreamWriter(requestStream);
writer.Write(json);
writer.Flush();
}
using (var resp = await request.GetResponseAsync())
{
using (var responseStream = resp.GetResponseStream())
{
var reader = new StreamReader(responseStream);
return = reader.ReadToEnd();
}
}
}

Categories

Resources