How do I convert a string into an HTTP Response in C#? - c#

{
"AdditionalProcessCardSwipeResponseData": null,
"CustomerTransactionID": "",
"ProcessCardSwipeOutputs": [
{
"AdditionalProcessCardSwipeResponseData": null,
"CardSwipeOutput": {
"AdditionalOutputData": [
{
"key": "CardType",
"value": "VISA"
}
],
"CardID": "abcdefghijk",
"IsReplay": false,
"MagnePrintScore": 0.12345,
"PanLast4": "1234"
},
"CustomerTransactionID": "",
"DecryptForwardFaultException": null,
"MagTranID": "2c3b08e9-b628-4f3c-a8ad-1ac1d57c1698",
"PayloadResponse": "HTTP\/1.1 200 OKPragma: no-cache\u000aX-OPNET-Transaction-Trace: a2_8bfb4474-c9fb-4257-b914-8411770544e4-22192-26834262\u000aAccess-Control-Allow-Credentials: true\u000aAccess-Control-Allow-Headers: x-requested-with,cache-control,content-type,origin,method,SOAPAction\u000aAccess-Control-Allow-Methods: PUT,OPTIONS,POST,GET\u000aAccess-Control-Allow-Origin: *\u000aStrict-Transport-Security: max-age=31536000\u000aX-Cnection: close\u000aContent-Length: 328\u000aCache-Control: no-store\u000aContent-Type: application\/json; charset=utf-8\u000aDate: Thu, 26 Dec 2019 16:05:35 GMT\u000a\u000a&{\"messages\":{\"resultCode\":\"Error\",\"message\":[{\"code\":\"E00003\",\"text\":\"The 'AnetApi\/xml\/v1\/schema\/AnetApiSchema.xsd:customerProfileId' element is invalid - The value 'customer_profile_id' is invalid according to its datatype 'AnetApi\/xml\/v1\/schema\/AnetApiSchema.xsd:numericString' - The Pattern constraint failed.\"}]}}",
"PayloadToken": "ADFASDFASDFASDFASDFASFADSFF",
"TransactionUTCTimestamp": "2019-12-26 16:05:35Z"
}
]
}
How do I convert the string returned for "PayloadResponse" to a HTTPResponse? I've tried the following but am not able to retrieve the body of the response:
var response = JObject.Parse(await httpResponseMessage.Content.ReadAsStringAsync());
var payloadResponse = response["ProcessCardSwipeOutputs"][0]["PayloadResponse"];
var msg = new HttpResponseMessage
{
Content = new StringContent(payloadResponse.ToString(), Encoding.UTF8, "application/json")
};
This is the content of the PayloadResponse I want to convert to an HttpResponse so that I can parse out the response body in a clean way:
HTTP/1.1 200 OKPragma: no-cache
X-OPNET-Transaction-Trace: a2_cadac737-0b60-45f5-9d5a-4d540c0975a0-7760-47076038
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: x-requested-with,cache-control,content-type,origin,method,SOAPAction
Access-Control-Allow-Methods: PUT,OPTIONS,POST,GET
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000
X-Cnection: close
Content-Length: 530
Cache-Control: no-store
Content-Type: application/json; charset=utf-8
Date: Thu,
26 Dec 2019 21: 46: 56 GMT
&{
"customerProfileId": "45345345345",
"customerPaymentProfileId": "123123123",
"validationDirectResponse": "1,1,1,(TESTMODE) This transaction has been approved.,000000,P,0,none,Test transaction for ValidateCustomerPaymentProfile.,1.00,CC,auth_only,none,John,Doe,,2020 Vision St,Somewhere,CA,90028,USA,,,email#example.com,,,,,,,,,0.00,0.00,0.00,FALSE,none,,,,,,,,,,,,,,XXXX1234,Visa,,,,,,,,,,,,,,,,,",
"messages": {
"resultCode": "Error",
"message": [
{
"code": "E00039",
"text": "A duplicate customer payment profile already exists."
}
]
}
}

If I understand correctly you just want to "parse out the response body in a clean way".
You are trying to convert this to an HttpResponseMessage because you think that will line everything up for you. This is a distraction, it makes it sound like you want to create a response and forward it on, but all you really want is the payload to be parsed into a usable format.
Correct me if I'm wrong.
To parse out that payload you can split that string on the newline character (/u000a), remove the extraneous & and parse the json.
var splitResponse = payloadResponse.ToString().Split(new char[] { '\u000a' });
string body = splitResponse.Last().Substring(1);
JObject job = JObject.Parse(body);
// example
Console.WriteLine(job["messages"]["message"][0]["text"]);
I did not provide classes that you can deserialize this json into because it is an error message and I assume you won't always be dealing with an error. A success response would probably be a different schema. I can't know how to design classes for this from the information you have provided but maybe working with the JObject is adequate.

Related

Make POST request

I'm trying to post to a web form defined as:
<form name="frmdata" method='post' enctype ='multipart/form-data' action ="http://www.rzp.cz/cgi-bin/aps_cacheWEB.sh">
<input type ="hidden" name ="VSS_SERV" value="ZVWSBJXML">
<input type="file" name="filename">
<input type ='submit' name ='x' value ='ODESLI'>
</form>
There is some additional documentation on the form here:
http://www.rzp.cz/docs/RZP02_XML_28.pdf
My latest try:
using (WebClient client = new WebClient())
{
NameValueCollection vals = new NameValueCollection();
vals.Add("VSS_SERV", "ZVWSBJXML");
string filecontent = #"<?xml version=""1.0"" encoding=""ISO-8859-2""?>";
filecontent = filecontent + #"
<VerejnyWebDotaz
elementFormDefault=""qualified""
targetNamespace=""urn:cz:isvs:rzp:schemas:VerejnaCast:v1""
xmlns=""urn:cz:isvs:rzp:schemas:VerejnaCast:v1"" version=""2.8"">";
filecontent = filecontent + #"
<Kriteria>
<IdentifikacniCislo>03358437</IdentifikacniCislo>
<PlatnostZaznamu>0</PlatnostZaznamu></Kriteria>";
filecontent = filecontent + #"</VerejnyWebDotaz>";
vals.Add("filename", filecontent);
client.Headers["ContentType"] = "multipart/form-data";
byte[] responseArray = client.UploadValues(#"http://www.rzp.cz/cgi-bin/aps_cacheWEB.sh", "POST", vals);
string str = Encoding.ASCII.GetString(responseArray);
}
But I can't get past this error:
<KodChyby>-1</KodChyby> (the xml filename does not contain xml defined by namespace)
How can I send this xml data to the form or rather there is a working form - http://stuff.petrovsky.cz/subdom/stuff/RZP/rzp-test-form.php - how to call and catch xml data? I would like to do the same request and get xml.
Using System.Net.Http I was able to construct the form request as a proof of concept using MultipartFormDataContent
Now initially when I tested it, I received 403 Forbidden response but I guessed that was to be expected given my location and that the endpoint might be region locked.
Raw Fiddler response
HTTP/1.1 403 Forbidden
Date: Sat, 27 Oct 2018 01:37:09 GMT
Server: IIS
Content-Length: 225
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /cgi-bin/aps_cacheWEB.sh
on this server.</p>
</body></html>
I was wrong and the forbidden appeared to be the default response for bad requests as you commented that you received the same forbidden error from within the region. So back to the drawing board I went.
I then copied the example HTML form locally and then proceeded to compare the requests from the form (which did actually work) and my code. Gradually making changes to match I was finally able to get a 200 OK response, but the body of the response was empty.
Apparently there was an issue with the server interpreting the boundary in the content type header if it is wrapped in quotes boundary="...".
After more adjustments it then started returning a message based on the content dispositions generated.
HTTP/1.1 200 OK
Date: Sat, 27 Oct 2018 19:55:11 GMT
Server: IIS
Serial: 10.145
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain; charset=ISO-8859-2
Content-Length: 169
Multiple definitions of VSS_SERV encountered in input.
If you're trying to do this intentionally (such as with select),
the variable must have a "List" suffix.
So it turns out that the XML API is expecting the request to be in a very specific format. Deviate from that and the request fails.
The MultipartFormDataContent was not generating the request correctly and this caused the server to not behave as expected. Other headers were being placed before the Content-Disposition headers of the parts and the Content-Disposition parameters were also not being enclosed in quotes. So by not including the content-type it in the parts and making sure the content-disposition headers were generated correctly eventually fixed the problem.
It is important to note the order of how the headers are added to the content so that the Content-Disposition header is set first for each part.
Working Code that generates the request in the desired format and gets the XML data.
[Test]
public async Task Post_Form() {
//Arrange
var stream = getXml();
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = #"""filename""",
FileName = #"""req-details.xml""",
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
var stringContent = new ByteArrayContent(Encoding.UTF8.GetBytes("ZVWSBJXML"));
stringContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = #"""VSS_SERV""",
};
//could have let system generate it but wanteed to rule it out as a problem
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
var form = new MultipartFormDataContent(boundary);
//FIX: boundary quote issue
var contentType = form.Headers.ContentType.Parameters.First(o => o.Name == "boundary");
contentType.Value = contentType.Value.Replace("\"", String.Empty);
form.Add(stringContent);
form.Add(fileContent);
//var data = await form.ReadAsStringAsync(); //FOR TESTING PORPOSES ONLY!!
var client = createHttpClient("http://www.rzp.cz/");
//Act
var response = await client.PostAsync("cgi-bin/aps_cacheWEB.sh", form);
var body = await response.Content.ReadAsStringAsync();
//Assert
response.IsSuccessStatusCode.Should().BeTrue();
body.Should().NotBeEmpty();
var document = XDocument.Parse(body); //should be valid XML
document.Should().NotBeNull();
}
The code above generated the following request, which I extracted using fiddler (Pay close attention to the working format)
POST http://www.rzp.cz/cgi-bin/aps_cacheWEB.sh HTTP/1.1
User-Agent: System.Net.Http.HttpClient
Accept-Language: en-US, en; q=0.9
Accept: text/xml, application/xml
Cache-Control: max-age=0
Content-Type: multipart/form-data; boundary=---------------------------8d63c301f3e044f
Host: www.rzp.cz
Content-Length: 574
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
-----------------------------8d63c301f3e044f
Content-Disposition: form-data; name="VSS_SERV"
ZVWSBJXML
-----------------------------8d63c301f3e044f
Content-Disposition: form-data; name="filename"; filename="req-details.xml"
Content-Type: text/xml
<?xml version="1.0" encoding="iso-8859-2"?>
<VerejnyWebDotaz xmlns="urn:cz:isvs:rzp:schemas:VerejnaCast:v1" version="2.8">
<Kriteria>
<IdentifikacniCislo>75848899</IdentifikacniCislo>
<PlatnostZaznamu>0</PlatnostZaznamu>
</Kriteria>
</VerejnyWebDotaz>
-----------------------------8d63c301f3e044f--
Which was able to get the following response.
HTTP/1.1 200 OK
Date: Sat, 27 Oct 2018 21:17:50 GMT
Server: IIS
Serial: 10.145
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/xml;charset=ISO-8859-2
Content-Length: 931
<?xml version='1.0' encoding='iso-8859-2'?>
<VerejnyWebOdpoved xmlns="urn:cz:isvs:rzp:schemas:VerejnaCast:v1" version="2.8">
<Datum>27.10.2018</Datum>
<Kriteria>
<IdentifikacniCislo>75848899</IdentifikacniCislo>
<PlatnostZaznamu>0</PlatnostZaznamu>
</Kriteria>
<PodnikatelSeznam>
<PodnikatelID>212fbf8314e01506b0d7</PodnikatelID>
<ObchodniJmenoSeznam Popis="Jméno a příjmení:">Filip Zrůst</ObchodniJmenoSeznam>
<IdentifikacniCisloSeznam Popis="Identifikační číslo osoby:">75848899</IdentifikacniCisloSeznam>
<TypPodnikatele Popis="Typ podnikatele:">Fyzická osoba</TypPodnikatele>
<AdresaPodnikaniSeznam Popis="Adresa sídla:">Vlašská 358/7, 118 00, Praha 1 - Malá Strana</AdresaPodnikaniSeznam>
<RoleSubjektu Popis="Role subjektu:">podnikatel</RoleSubjektu>
<EvidujiciUrad Popis="Úřad příslušný podle §71 odst.2 živnostenského zákona:">Úřad městské části Praha 1</EvidujiciUrad>
</PodnikatelSeznam>
</VerejnyWebOdpoved>
From there it should be small work to parse the resulting XML as needed.
Supporting code
Generate or load the stream of the XML for the form
private static Stream getXml() {
var xml = #"<?xml version=""1.0"" encoding=""ISO-8859-2""?>
<VerejnyWebDotaz
xmlns=""urn:cz:isvs:rzp:schemas:VerejnaCast:v1""
version=""2.8"">
<Kriteria>
<IdentifikacniCislo>75848899</IdentifikacniCislo>
<PlatnostZaznamu>0</PlatnostZaznamu>
</Kriteria>
</VerejnyWebDotaz>";
var doc = XDocument.Parse(xml);//basically to validate XML
var stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;
return stream;
}
Was gradually able to whittle down the headers needed for a successful request after find the match that worked. Try removing others gradually to test if more can be removed safely to reduce the amount of unnecessary code needed.
private static HttpClient createHttpClient(string baseAddress) {
var handler = createHandler();
var client = new HttpClient(handler);
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "System.Net.Http.HttpClient");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.9");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/xml,application/xml");
client.DefaultRequestHeaders.ExpectContinue = false;
client.DefaultRequestHeaders.ConnectionClose = false;
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue() {
MaxAge = TimeSpan.FromSeconds(0)
};
return client;
}
private static HttpClientHandler createHandler() {
var handler = new HttpClientHandler();
// if the framework supports automatic decompression set automatic decompression
if (handler.SupportsAutomaticDecompression) {
handler.AutomaticDecompression = System.Net.DecompressionMethods.GZip |
System.Net.DecompressionMethods.Deflate;
}
return handler;
}
While I chose to use the asynchronous API of System.Net.Http, I found a similar question
Reference UploadFile with POST values by WebClient
With an answer that was done using WebClient that could be adapted to your question so that a request can be constructed similar to what was produced above.
I tried testing that one as well but got into the same forbidden error. Now that the correct format is know you should also be able to correctly craft a working request using WebClient/WebRequest

Web api error 500 starting from usermanager

First of all, I would like to apologize for my really bad english, if something is not understandable, then reask please.
When I try POST with /api/account/regapi in Swagger then with too short password it gives error 200, but with correct length it gives error 500, while debugging it shows correct parameters at AuthDTO model, but still shows error 500 with correct data.
Response header returned by false data(incorrect password length):
{
"$id": "1",
"succeeded": false,
"errors": [
{
"$id": "2",
"code": "PasswordTooShort",
"description": "Passwords must be at least 6 characters."
}
]
}
Response header with incorrect password
access-control-allow-origin: *
content-type: application/json; charset=utf-8
date: Sat, 26 May 2018 13:54:06 GMT
server: Kestrel
transfer-encoding: chunked
x-powered-by: ASP.NET
x-sourcefiles: =?UTF-8?B?RDpcS29vbGkgQXNqYWRcVlIyXFZSMlByb2pla3RTb2x1dGlvblxWUjJQcm9qZWt0XGFwaVxBY2NvdW50XFJlZ0FwaQ==?=
Response header with correct data:
content-type: text/html; charset=utf-8
date: Sat, 26 May 2018 14:03:59 GMT
server: Kestrel
transfer-encoding: chunked
x-powered-by: ASP.NET
x-sourcefiles: =?UTF-8?B?RDpcS29vbGkgQXNqYWRcVlIyXFZSMlByb2pla3RTb2x1dGlvblxWUjJQcm9qZWt0XGFwaVxBY2NvdW50XFJlZ0FwaQ==?=
Registration in AccountController
public async Task<IActionResult> RegApi(AuthDTO model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
return Ok(result);
}
return BadRequest();
}
public class AuthDTO
{
public string Email { get; set; }
public string Password { get; set; }
}
I think the is problem is in _usermanager, because error 500 is starting to show up there

How to call Web API (App Service) remotely

I need to call an API from AppService by uri.
This is my API:
public ApiOutputBase Test_AddStudent(string name, int age, string address)
{
return new ApiOutputBase
{
Result = new Result { Status = true, Message = "OK,Test_AddStudent Done!" },
OuputValues = new List<object>() { name, age, address }
};
}
I use this Function to call it:
public async Task<bool> TestCallApi()
{
var client = new HttpClient { BaseAddress = new Uri("http://localhost/") };
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var testJson = "{\r\n \"name\": \"MyName\",\r\n \"age\": 25,\r\n \"address\": \"MyAddress\"\r\n}";
HttpResponseMessage response = await client.PostAsync("api/services/myApp/commonLookup/Test_AddStudent", new StringContent(testJson));
// Call api success
if (response.IsSuccessStatusCode)
{
}
return true;
}
I used Swagger to call Test_AddStudent successfully. The testJson was copied from Swagger when I call Test_AddStudent successfully.
After that, I used Swagger to call TestCallApi without any error, but when I tried to debug the value of HttpResponseMessage, it showed this error:
{
StatusCode: 400,
ReasonPhrase: 'Bad Request',
Version: 1.1,
Content: System.Net.Http.StreamContent,
Headers: {
Pragma: no-cache
Cache-Control: no-store, no-cache
Date: Tue, 31 Oct 2017 02:12:45 GMT
Set-Cookie: Abp.Localization.CultureName=en; expires=Thu, 31-Oct-2019 02:12:45 GMT; path=/
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 405
Content-Type: application/json; charset=utf-8
Expires: -1
}
}
Have I missed something?
I finally found the root cause: I passed the wrong input to the api:
Wrong:
var testJson = "{\r\n \"name\": \"MyName\",\r\n \"age\": 25,\r\n \"address\": \"MyAddress\"\r\n}";
HttpResponseMessage response = await client.PostAsync("api/services/myApp/commonLookup/Test_AddStudent", new StringContent(testJson));
Correct:
HttpResponseMessage response = await client.PostAsync("api/services/myApp/commonLookup/Test_AddStudent?name=MyName&age=25&address=MyAdress", "");

C# HttpClient PostAsJsonAsync -> Error reading string

Hi I try to call an web api by server side with:
using (var client = new HttpClient())
{
using (var rsp = client.PostAsJsonAsync<Request>(url, model).Result)
{
if (!rsp.IsSuccessStatusCode)
{
// throw an appropriate exception
}
var result = rsp.Content.ReadAsAsync<string>().Result;
}
}
but I get error
Error reading string. Unexpected token: StartObject. Path '', line 1, position 1.
If I try to call same url from jQuery
$.post('http://localhost/api/Test')
the server return
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 25 Oct 2015 12:15:56 GMT
Content-Length: 104
{
"Header": {
"Token": "Response",
"Timestamp": "2015-10-25T14:15:56.0092197+02:00"
}
}
The "model" arrive on api controller but I can't get response from request.
ReadAsAsync<T> attempts to deserialize the response to type T. In this case, you're saying you want to deserialize JSON to a string, which doesn't really make sense. Either use a type matching the response (i.e. a custom data structure containing Header, Token, etc.) or use ReadAsStringAsync() if you really want to get a string.

YouTube API Video Upload Parse Error in C#

I'm trying to upload a video (resumable) to YouTube via the API in C# (not through client).
I'm following these instructions on using resumable upload protocol
However when I make the attempt I get back an Parse Error.
Here is my request:
POST /upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails&key={api_key} HTTP/1.1
Host: www.googleapis.com
X-upload-content-length: 5346742
X-upload-content-type: video/*
Content-type: application/json; charset=UTF-8
Content-length: 277
Authorization: Bearer {access_token}
{
"snippet": {
"title": "My video title",
"description": "This is a description of my video",
"tags": ["cool", "video", "more keywords"],
"categoryId": 22
},
"status": {
"privacyStatus": "public",
"embeddable": True,
"license": "youtube"
}
}
Here is the response:
HTTP/1.1 400 Bad Request
Alternate-protocol: 443:quic
Content-length: 171
Via: HTTP/1.1 GWA
X-google-cache-control: remote-fetch
Server: HTTP Upload Server Built on Sep 30 2013 10:58:35 (1380563915)
Date: Wed, 02 Oct 2013 21:38:10 GMT
Content-type: application/json
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
Anyone have any ideas?
The parse error is due to True being written with a big T.
(using the beta library is not an option for me becuase of Unity. .NET 2.0..)
why dont u use Google GData Youtube Api for .NET ?
string developerkey = "developerKey";
YouTubeRequestSettings settings = new YouTubeRequestSettings("Somethinghere", developerkey, "googleUserName", "googlePassWord");
YouTubeRequest request = new YouTubeRequest(settings);
newVideo.Title = "Video Title Here || ArgeKumandan";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "cars, funny";
newVideo.Description = "My description";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));
// alternatively, you could just specify a descriptive string
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(#"D:\video ArgeKumandan.flv", "video/flv");
createdVideo = request.Upload(newVideo);

Categories

Resources