Accessing Request Object in WebAPI 2 - asp.net-web-api2

I am ruining into ModelBinding Error in WebAPI, I see that primary Key is being Posted back to the Server, but on the Server in the Model Property that Key is always empty.
I decided to inspect it further and see if the Request Object is able to retrieve that value if so i will go ahead and implement my own custom Model Binder. But know i ran into another problem where i cannot figure out howto extract the value using Request Object ?
This should have been fairly simple , but cannot find correct way ?
Here is the Postinformation, with OrgCode_PK being the property giving problem.
Request URL:http://localhost:1398/api/Org/PutOrg
Request Method:PUT
Status Code:404 Primary Key Not found
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:473
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:sbOverlayID=52335384; __iswl_localhost:1398=0; __ctxpop=1
Host:localhost:1398
Origin:http://localhost:1398
Referer:http://localhost:1398/main.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
$inlinecount:allpages
OrgCode_PK:ORG6
OrgHeadCode_FK:ORGH12
Code:CBSE-KHN
Name:khkl modified
Description:jhjkh
Address1:hjkh
Address2:jkhkj
CountryCode_FK: edf
CountryName:abyu
StateCode_FK:klh
StateName:yhu
CityCode_FK:hjkh
CityName:khk
ZIP:67u8
RowStatusCode_FK:NEW
RowStatus:NEW ROW
DateCreated:02-06-2014
DateUpdated:02-06-2014
EffectiveDate:02-11-2014
TerminationDate:05-21-2014
CreatedByUserName:jkhkjhkj
UpdatedByUserName:kjkljlk
RowStatusName:NEW ROW
Response Headersview source
Cache-Control:no-cache
Content-Length:41
Content-Type:text/plain; charset=utf-8
Date:Fri, 07 Feb 2014 17:24:52 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcUHJvamVjdHNcU01XaXRoQXV0aGVudGljYXRpb25cU000XFNNNFxhcGlcT3JnXFB1dE9yZw==?=

Found this myself
var httpContext = (HttpContextWrapper)Request.Properties["MS_HttpContext"];
var OrgCodePK = httpContext.Request.Form["OrgCode_PK"];

Related

Telegram Bot API: getChatMember throws USER_ID_INVALID for valid user

I'm trying to find out if a specific User is present in a supergroup, in order to keep track of those who left.
For that, I'm calling the Bot API method getChatMember for each User and checking if their status is either Left or Kicked. However, I noticed that (recently?) I'm getting USER_ID_INVALID errors for many valid users that are either in the supergroup or have been in the past and then left. I also confirmed that those accounts are still active on Telegram.
Here's the HTTP request I'm sending:
POST https://api.telegram.org/botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXX/getChatMember HTTP/1.1
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 46
Host: api.telegram.org
{"chat_id":-0000000000000,"user_id":000000000}
And here's the response I'm getting:
HTTP/1.1 400 Bad Request
Server: nginx/1.12.2
Date: Fri, 20 Apr 2018 04:17:32 GMT
Content-Type: application/json
Content-Length: 74
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
{"ok":false,"error_code":400,"description":"Bad Request: USER_ID_INVALID"}
Any way I look at it, it looks like a perfectly valid request to me. And I haven't been able to find a common pattern between the users that throw this error.
What am I missing here?
EDIT: As #sean pointed out, having one of those users message the bot privately fixed the error for that particular user. But I'm absolutely sure that user was seen before because that's how I got his user ID. What could have caused the bot "forget" about him and how would I prevent this from happening in the future?
This error means your bot haven't seen this user before.
For instance, my user ID is 109780439, you can try getChatMember with #PublicTestGroup, it should response with 400 error.
And then, forward ANY of my message (e.g., this) to your bot, you will see the different result :)
You will create a variable who get your channel's result, like this:
$join : api.telegram.org/botYOURTOKEN/getchat .....
if($message && (strpos($join,'"status":"left"') or strpos($join,'"Bad Request: USER_ID_INVALID"') or strpos($join,'"status":"kicked"'))!== false) {
}

WebRequest.GetResponse - The remote server returned an error: (404) Not Found

I am trying to connect to a website but it keeps returning this error even though i can reach the website in my browser:
An exception of type 'System.Net.WebException' occurred in System.dll
but was not handled in user code
Additional information: The remote server returned an error: (404) Not
Found.
I'm pretty sure my code is correct as I've used the same code a lot recently but cannot work out why it returning an error, any suggestions?
My Code:
OddsTodayREQUEST = WebRequest.Create("http://www.betexplorer.com/next/soccer/")
Using OddsTodayRESPONSE As WebResponse = OddsTodayREQUEST.GetResponse()
Using OddsTodayREADER As New StreamReader(OddsTodayRESPONSE.GetResponseStream())
OddsTodayHTML = OddsTodayREADER.ReadToEnd()
End Using
End Using
The site wants a User Agent added to the request. You can google What's my user agent? to find your own and add it like this:
OddsTodayREQUEST.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)"
You need to add UserAgent as #ChaseRocker mentioned, In addition to his answer, It's better to use AutomaticDecompression property of HttpWebClient and you may add Accept header. I also used OddsTodayRESPONSE.GetResponseStream() in Using statement.
Dim OddsTodayREQUEST As HttpWebRequest = WebRequest.Create("http://www.betexplorer.com/next/soccer/")
OddsTodayREQUEST.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
OddsTodayREQUEST.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate 'Decompressing makes the request be done faster
OddsTodayREQUEST.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0"
Using OddsTodayRESPONSE As HttpWebResponse = OddsTodayREQUEST.GetResponse()
Using OddsTodayRESPONSESTREAM = OddsTodayRESPONSE.GetResponseStream()
Using OddsTodayREADER As New StreamReader(OddsTodayRESPONSESTREAM)
OddsTodayHTML = OddsTodayREADER.ReadToEnd()
End Using
End Using
End Using

Html5, chuncked video streaming

My html-video calls multiple separate request for chunks. seems not like single stream.
When I see that in debugging tools,
As you see, there are 3 different call.
This is the request header,
Accept:*/*
Accept-Encoding:identity;q=1, *;q=0
Accept-Language:ja-JP,en-US;q=0.8
Connection:keep-alive
Cookie:stg_domain_token=oNijQNByftcYnsLGzFZxRyCesLR-GdWKi6a-uKSJJ9060Yk8pwCiUlcHChyf
Host:stg.myhost.com
Range:bytes=32768-
User-Agent:Mozilla/5.0 (Linux; Android 6.0.1; SC-05G Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/54.0.2840.68 Mobile Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:62626f5b-82c9-48b9-97f5-a7a983e1c3bc
and here is the response header,
accept-ranges:bytes
Connection:keep-alive
Content-Disposition:filename=49976265106__9BB3FA25-04E4-4AF5-903C-9B12CF622567.MOV
Content-Length:324882
content-range:bytes 32768-357649/357650
Content-Type:video/quicktime
Date:Fri, 04 Nov 2016 06:15:06 GMT
Server:Apache
X-Powered-By:PHP/5.6.17
anyone know what I am missing?
Browser won't download entire video or audio file at a time. It downloads them in chunks and plays them one after another.
For your understanding, I'm explaining the headers here.
Request Header
Accept:*/* : Browser will accept any MIME-Types as response.
Range:bytes=32768- : Browser already has the video part, till byte 32767 but requires file from byte 32768.
Response Header
status : 206 : It means the served content is partial (not complete file)
accept-ranges:bytes : Server accepts byte ranges only (which is universal)
Content-Length:324882 : Total content length from requested byte.
content-range:bytes 32768-357649/357650: it is in this format start byte - last byte / total length (from 0 byte to end)
Content-Type:video/quicktime : Type of content

SoapUI: Transfer Response Payload Value to New Request (Different Test Steps)

I'm trying to figure out SoapUI, and so far it's been a great tool. However, I cannot figure out this transferring of property stuff. I've read so much and just can't seem to find the answer I'm looking for.
I have one request: TC01_vorbereitenKunde
I receive the following Response Payload back:
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
SOAPAction: "http://..."
Accept: text/xml
Content-Type: text/xml; charset=UTF-8
Content-Language: en-US
Date: Tue, 22 Sep 2015 15:58:01 GMT
Content-Length: 414
Content-Encoding: gzip
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<nova-kunden:vorbereitenKundeResponse xmlns:nova-kunden="http://...>
<nova-kunden:vorbereitungKundeAntwort>
<status>true</status>
<tkid>31f64d0f-b076-4304-95ab-15cb0de38adb</tkid>
<meldungen/>
</nova-kunden:vorbereitungKundeAntwort>
</nova-kunden:vorbereitenKundeResponse>
</soapenv:Body>
I then want to take the "tkid" value and place it in the following request: TC02_offeriereLeistungen
I've tried: ${TC01_vorbereitenKunde#Response#//tkid}
"TC01_vorbereitenKunde" is the name of the Test Step where the response payload is from to no avail.
What am I missing? Thank you so much in advance for your help!
Have script assertion for the first step as given below:
import com.eviware.soapui.support.XmlHolder
def xml = new XmlHolder(context.response)
def responseValue = xml.getNodeValue("//*:tkid")
assert null != responseValue, "Response does not have value"
context.testCase.setPropertyValue('TK_ID', responseValue)
In the second step, use ${#TestCase#TK_ID} where value is needed.

Rails UTF-8 response

I've got a Rails 3.2 app running on Ruby 1.9.3 that returns JSON data stored in a MongoDB database. The data seems to be stored correctly in mongo, e.g. (look at the name attribute):
{ "_id" : ObjectId("4f986cbe4c8086fdc9000002"), "created_at" : ISODate("2012-04-25T21:31:45.474Z"), "updated_at" : ISODate("2012-04-26T22:07:23.901Z"), "creator_id" : ObjectId("4f6b4d3c4c80864381000001"), "updater_id" : null, "name" : "Trädgår'n", "sort" : "tradgarn", "address" : "Nya Allén 11", "coordinates" : [ 11.9764791, 57.7045625 ], "phone" : "46031102080", "url" : "http://www.profilrestauranger.se/tradgarn/", "user_ids" : [ ] }
But when I issue a request that returns this record, I get something like this back (now look at the name attribute):
{"address":"Nya All\u00e9n 11","coordinates":[11.9764791,57.7045625],"created_at":"2012-04-25T23:31:45+02:00","id":"4f986cbe4c8086fdc9000002","name":"Tr\u00e4dg\u00e5r'n","phone":"46031102080","sort":"tradgarn","updated_at":"2012-04-27T00:07:23+02:00","url":"http://www.profilrestauranger.se/tradgarn/"}
The response headers for anyone interested:
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Thu, 26 Apr 2012 22:41:13 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 909
Connection: keep-alive
Status: 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *,x-requested-with
X-UA-Compatible: IE=Edge
ETag: "d2a95f06bec10d8087c3188280292d3c"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: fdd042568195df279e59affe45bdcd37
X-Runtime: 0.037134
I cannot seem to figure out why or where the encoding is getting messed up? What gives? Help? :)
The issue is indeed one of JSON UTF-8 encoding. The #to_json method is escaping unicode characters. This can be observed by something like:
user.to_json
# => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"tr\\u00e4dg\\u00e5r#example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"
When parsed, however, this is converted back to how you would expect it. It is possible, however, to generate the JSON using JSON.generate, with which the #as_json method can be used, along with any options for the construction. This doesn't escape the unicode. To do such:
JSON.generate(user.as_json)
# => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"trädgår#example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"
Turns out the problem I was seeing was with the gem colorful_json. I was running the JSON thru its CLI utility cjson, and it was messing up the Unicode. I reported the issue and the new version of the gem fixes this.